麻烦帮忙看下这个程序出现什么问题了呢?运行是报“出错啦!”

问题描述

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还是要出错

时间: 2024-09-17 03:30:56

麻烦帮忙看下这个程序出现什么问题了呢?运行是报“出错啦!”的相关文章

c语言-求帮忙看下为什么程序调用了西沟函数导致程序整个不能运行

问题描述 求帮忙看下为什么程序调用了西沟函数导致程序整个不能运行 写说明一下代码,代码主要是要在控制台模拟浏览器对tab和网页前进后退的一些操作 再说明一下问题,问题在于brwosertab类和webinformation类的西沟函数,一旦在这两个类的西沟函数里面写上delete 指针名字:整个程序就不能运行了,最关键的是我不太明白为什么程序要调用这两个函数,因为我感觉我都是动态对象啊.最后说明一下,我很菜,只有一点java编程的经验,所以第一次应对c++我真的需要一些帮助.谢谢你们了! 解决方

spring和mybatis集成时总报错 麻烦帮忙看下什么原因

问题描述 spring和mybatis集成时总报错 麻烦帮忙看下什么原因 项目请求数据的时候报Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.business.dao.system.AdminMapper.selectByPrimaryKey麻烦大神帮忙看下这是啥原因,找了好久不知道

各位大神麻烦帮忙看下SQL对不对

问题描述 各位大神麻烦帮忙看下SQL对不对 parameterType="String"> SELECT receive_car_code, use_time, sc.coupon_name FROM car_coupon cc LEFT JOIN (SELECT * FROM sys_coupon WHERE coupon_id IN(SELECT coupon_id FROM sys_coupon_company WHERE 1=1 AND company_id=#{comp

内联-麻烦帮忙看下这个SQL查询该怎么改进

问题描述 麻烦帮忙看下这个SQL查询该怎么改进 有三张表,分别是EquipmentMessage,Sensor,SensorInputData,Sensor以 EquipmentMessage的主键equipmentId做外键,SensorInputData则以Sensor的主键sensorId做外键.SensorInputData是大表,可能有千万条数据,另外两个都是小表,最多不会超过200条数据.我的这个查询第二句是一个"*"号,此时查询速度很快,大概170ms,如果我只想要其中个

android-[Android]ExpandableListView消息响应出错!麻烦帮忙看下!

问题描述 [Android]ExpandableListView消息响应出错!麻烦帮忙看下! final ExAdapter listAdapter = new ExAdapter(MeterOperateActivity.this); buttonListView.setAdapter(listAdapter); buttonListView.setGroupIndicator(null); buttonListView.setDivider(null); buttonListView.set

JS冲突问题,麻烦帮忙看下。

问题描述 我的树形菜单用的是带记忆功能的JS代码同时还要用到漂浮的JS代码现在的问题是菜单的可以正常试用漂浮的代码不能跟随页面上下滚动不知道问题在那里麻烦知道的帮忙看下.先谢谢了.本人纯菜鸟,麻烦说的详细点.或者直接告诉我怎么改也可以.这个是菜单的代码<scriptlanguage="javascript"type="text/javascript">//---获取ClassNamedocument.getElementsByClassName=func

帮忙看下这个程序,例如输出144.42,最后救过是一百四十四元四角一分,最后一位总是出错!

问题描述 packageyy;importjava.util.Scanner;publicclassRMB{publicstaticvoidmain(String[]args){System.out.println("请输入整数部位不超过九位的实数:");String[]number=newString[10];number[0]="零";number[1]="壹";number[2]="贰";number[3]="

麻烦帮忙看下这段多线程机制是否有问题

问题描述 下面是自己封闭的list然后申明了一个同步List类.后面在程序中申明了这个类的一个静态对象如下:publicstaticJobQueuequeue=newJobQueue(1000);然后程序中放数据跟取数据分别用的是下面的方法放数据:SendControlThread.queue.addJob(cmop);取数据:objEx=SendControlThread.queue.getJob();请问会不会出现捞数据的线程跟发送的线程都wait的情况或者还是其他问题.一个捞取线程.6个取

xplanner部署tomcat启动报错,麻烦帮忙看下,错误信息贴出来如下

问题描述 [1235]ERROR-work.web.context.ContextLoader-Contextinitializationfailedorg.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'metaRepository'definedinclasspathresource[spring-beans.xml]:Errorsettingpropertyvalues;nested