java中怎样从文件中读取数据?

java中怎样从文件中读取数据?,第1张

分为读字节,读字符两种读法\x0d\x0a◎◎◎FileInputStream 字节输入流读文件◎◎◎\x0d\x0apublic class Maintest {\x0d\x0a\x0d\x0apublic static void main(String[] args) throws IOException {\x0d\x0a\x0d\x0aFile f=new File("G:\\just for fun\\xiangwei.txt")\x0d\x0a\x0d\x0aFileInputStream fin=new FileInputStream(f)\x0d\x0a\x0d\x0abyte[] bs=new byte[1024]\x0d\x0a\x0d\x0aint count=0\x0d\x0awhile((count=fin.read(bs))>0)\x0d\x0a{\x0d\x0a\x0d\x0aString str=new String(bs,0,count)//反复定义变量:每一次都 重新定义新变量,接收新读取的数据\x0d\x0a\x0d\x0aSystem.out.println(str)//反复输出新变量:每一次都 输出重新定义的新变量\x0d\x0a}\x0d\x0afin.close()\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a◎◎◎FileReader 字符输入流读文件◎◎◎\x0d\x0apublic class Maintest {\x0d\x0apublic static void main(String[] args) throws IOException {\x0d\x0a\x0d\x0aFile f=new File("H:\\just for fun\\xiangwei.txt")\x0d\x0a\x0d\x0aFileReader fre=new FileReader(f)\x0d\x0a\x0d\x0aBufferedReader bre=new BufferedReader(fre)\x0d\x0a\x0d\x0aString str=""\x0d\x0awhile((str=bre.readLine())!=null)//●判断最后一行不存在,为空\x0d\x0a{\x0d\x0aSystem.out.println(str)\x0d\x0a}\x0d\x0abre.close()\x0d\x0a fre.close()\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a}

java读取文本文件的方法有很多 这个例子主要介绍最简单 最常用的BufferedReader类 完整例子如下 package net chinaunix blog hzm textimport java io BufferedReaderimport java io FileReaderimport java io IOExceptionpublic class ReadFile {private String pathpublic ReadFile(String filePath){path = filePath}public String[] openFile() throws IOException{FileReader fr = new FileReader(path) BufferedReader textReader = new BufferedReader(fr) String[] textData = new String[readLines()]int ifor(i= i <readLines() i++){textData[i] = textReader readLine() }textReader close() return textData}int readLines() throws IOException{FileReader fileToRead = new FileReader(path) BufferedReader bf = new BufferedReader(fileToRead) int numberOfLines = @SuppressWarnings( unused )String oneLinewhile((oneLine = bf readLine()) != null){numberOfLines++}bf close() return numberOfLines}}package net chinaunix blog hzm textimport java io IOExceptionpublic class FileData {public static void main(String[] args) throws IOException{String filePath = C:/text txt try{ReadFile reader = new ReadFile(filePath) String[] content = reader openFile() int ifor(i= i<content lengthi++){System out println(content[i]) }}catch(IOException e){System out println( 异常信息 + e getMessage()) }}}java io BufferedReaderThe buffer size may be specified or the default size may be used The default is large enough for most purposes In general each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly such as FileReaders and InputStreamReaders For example BufferedReader in = new BufferedReader(new FileReader( foo in )) will buffer the input from the specified file Without buffering each invocation of read() or readLine() could cause bytes to be read from the file converted into characters and then returned which can be very inefficient Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader java io FileReaderFileReader is meant for reading streams of characters For reading streams of raw bytes consider using a FileInputStream lishixinzhi/Article/program/Java/hx/201311/26249


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12003083.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存