问题描述
importjava.io.*;publicclassfile1{publicstaticvoidmain(String[]args)throwsException{Filef1=newFile("E:\abc\汇编\abc.txt");FileInputStreamfis=newFileInputStream(f1);DataInputStreamdis=newDataInputStream(fis);try{System.out.println(dis.readUTF());}catch(Exceptione){System.out.println("出错啦!");}dis.close();}}
解决方案
解决方案二:
DataInputStream是用来写java里的数据类型的如:Integer,byte,char,String。你在瞎用,文本文件这么能样读呀?用Readerpublicstaticvoidmain(String[]args)throwsException{Filef1=newFile("E:\abc\汇编\abc.txt");FileInputStreamfis=newFileInputStream(f1);BufferedReaderreader=newBufferedReader(newInputStreamReader(fis,"utf-8"));//DataInputStreamdis=newDataInputStream(fis);try{for(Stringline;(line=reader.readLine())!=null;){System.out.println(line);}}catch(Exceptione){e.printStackTrace();System.out.println("出错啦!");}reader.close();}
解决方案三:
楼上的方法不错,其实还可以用FileChannelPS:楼主发帖能不能把代码放
中去呀,这样发出来难看死了~~
解决方案四:
一楼是常用的读写文件的用法,但不知道楼主想知道什么。
解决方案五:
学习,学习。
解决方案六:
我想问一下你的abc.txt文件中有什么文件abc.txt中的内容不可以是随便加人的内容,最好是用writeUTF方法写入的内容否则会出现EOFException,那么就会输出“出错了!”
解决方案七:
是1L说的那样,LZ可以看下这段代码importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.RandomAccessFile;importjava.io.Reader;publicclassReadFromFile{/***以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。*@paramfileName文件的名*/publicstaticvoidreadFileByBytes(StringfileName){Filefile=newFile(fileName);InputStreamin=null;try{System.out.println("以字节为单位读取文件内容,一次读一个字节:");//一次读一个字节in=newFileInputStream(file);inttempbyte;while((tempbyte=in.read())!=-1){System.out.write(tempbyte);}in.close();}catch(IOExceptione){e.printStackTrace();return;}try{System.out.println("以字节为单位读取文件内容,一次读多个字节:");//一次读多个字节byte[]tempbytes=newbyte[100];intbyteread=0;in=newFileInputStream(fileName);ReadFromFile.showAvailableBytes(in);//读入多个字节到字节数组中,byteread为一次读入的字节数while((byteread=in.read(tempbytes))!=-1){System.out.write(tempbytes,0,byteread);}}catch(Exceptione1){e1.printStackTrace();}finally{if(in!=null){try{in.close();}catch(IOExceptione1){}}}}/***以字符为单位读取文件,常用于读文本,数字等类型的文件*@paramfileName文件名*/publicstaticvoidreadFileByChars(StringfileName){Filefile=newFile(fileName);Readerreader=null;try{System.out.println("以字符为单位读取文件内容,一次读一个字节:");//一次读一个字符reader=newInputStreamReader(newFileInputStream(file));inttempchar;while((tempchar=reader.read())!=-1){//对于windows下,rn这两个字符在一起时,表示一个换行。//但如果这两个字符分开显示时,会换两次行。//因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。if(((char)tempchar)!='r'){System.out.print((char)tempchar);}}reader.close();}catch(Exceptione){e.printStackTrace();}try{System.out.println("以字符为单位读取文件内容,一次读多个字节:");//一次读多个字符char[]tempchars=newchar[30];intcharread=0;reader=newInputStreamReader(newFileInputStream(fileName));//读入多个字符到字符数组中,charread为一次读取字符数while((charread=reader.read(tempchars))!=-1){//同样屏蔽掉r不显示if((charread==tempchars.length)&&(tempchars[tempchars.length-1]!='r')){System.out.print(tempchars);}else{for(inti=0;i<charread;i++){if(tempchars[i]=='r'){continue;}else{System.out.print(tempchars[i]);}}}}}catch(Exceptione1){e1.printStackTrace();}finally{if(reader!=null){try{reader.close();}catch(IOExceptione1){}}}}/***以行为单位读取文件,常用于读面向行的格式化文件*@paramfileName文件名*/publicstaticvoidreadFileByLines(StringfileName){Filefile=newFile(fileName);BufferedReaderreader=null;try{System.out.println("以行为单位读取文件内容,一次读一整行:");reader=newBufferedReader(newFileReader(file));StringtempString=null;intline=1;//一次读入一行,直到读入null为文件结束while((tempString=reader.readLine())!=null){//显示行号System.out.println("line"+line+":"+tempString);line++;}reader.close();}catch(IOExceptione){e.printStackTrace();}finally{if(reader!=null){try{reader.close();}catch(IOExceptione1){}}}}/***随机读取文件内容*@paramfileName文件名*/publicstaticvoidreadFileByRandomAccess(StringfileName){RandomAccessFilerandomFile=null;try{System.out.println("随机读取一段文件内容:");//打开一个随机访问文件流,按只读方式randomFile=newRandomAccessFile(fileName,"r");//文件长度,字节数longfileLength=randomFile.length();//读文件的起始位置intbeginIndex=(fileLength>4)?4:0;//将读文件的开始位置移到beginIndex位置。randomFile.seek(beginIndex);byte[]bytes=newbyte[10];intbyteread=0;//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。//将一次读取的字节数赋给bytereadwhile((byteread=randomFile.read(bytes))!=-1){System.out.write(bytes,0,byteread);}}catch(IOExceptione){e.printStackTrace();}finally{if(randomFile!=null){try{randomFile.close();}catch(IOExceptione1){}}}}/***显示输入流中还剩的字节数*@paramin*/privatestaticvoidshowAvailableBytes(InputStreamin){try{System.out.println("当前字节输入流中的字节数为:"+in.available());}catch(IOExceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args){StringfileName="C:/temp/newTemp.txt";ReadFromFile.readFileByBytes(fileName);ReadFromFile.readFileByChars(fileName);ReadFromFile.readFileByLines(fileName);ReadFromFile.readFileByRandomAccess(fileName);}}二、将内容追加到文件尾部importjava.io.FileWriter;importjava.io.IOException;importjava.io.RandomAccessFile;/***将内容追加到文件尾部*/publicclassAppendToFile{/***A方法追加文件:使用RandomAccessFile*@paramfileName文件名*@paramcontent追加的内容*/publicstaticvoidappendMethodA(StringfileName,Stringcontent){try{//打开一个随机访问文件流,按读写方式RandomAccessFilerandomFile=newRandomAccessFile(fileName,"rw");//文件长度,字节数longfileLength=randomFile.length();//将写文件指针移到文件尾。randomFile.seek(fileLength);randomFile.writeBytes(content);randomFile.close();}catch(IOExceptione){e.printStackTrace();}}/***B方法追加文件:使用FileWriter*@paramfileName*@paramcontent*/publicstaticvoidappendMethodB(StringfileName,Stringcontent){try{//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件FileWriterwriter=newFileWriter(fileName,true);writer.write(content);writer.close();}catch(IOExceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args){StringfileName="C:/temp/newTemp.txt";Stringcontent="newappend!";//按方法A追加文件AppendToFile.appendMethodA(fileName,content);AppendToFile.appendMethodA(fileName,"appendend.n");//显示文件内容ReadFromFile.readFileByLines(fileName);//按方法B追加文件AppendToFile.appendMethodB(fileName,content);AppendToFile.appendMethodB(fileName,"appendend.n");//显示文件内容ReadFromFile.readFileByLines(fileName);}}
解决方案八:
我觉得说那么多闲话干嘛,lz要定位问题,把catch(Exceptione){System.out.println("出错啦!");}改成catch(Exceptione){e.printStack();<=====加这句System.out.println("出错啦!");}就好了,自己看是什么问题。为甚说人家读写方法不对呢?人家又不是问怎么读写文件;虽然文件结尾是txt,你怎么知道那个就是纯文本文件?WINDOWS的文件后缀又没有强制校验功能。如果文件根本就不存在呢?改成BufferedReader还是要出错