怎么用java读取txt文件,我的文件中是字母为什么读出的是数字

怎么用java读取txt文件,我的文件中是字母为什么读出的是数字,第1张

package pandyfile;

import javaio;

/

Created by IntelliJ IDEA

User: pandy

Date: 2006-04-16

Time: 14:29:23

To change this template use File | Settings | File Templates

/

public class FileUtil

{

/

取得文件内容

@return String

/

public static String getFileCnt(String filePath)

{

StringBuffer content = new StringBuffer();

try

{

File temFile = new File(filePath);

FileReader fileReader = new FileReader(temFile);

BufferedReader bufferReader = new BufferedReader(fileReader);

String str = null;

while ((str = bufferReaderreadLine()) != null)

{

contentappend(str)append("\n");

}

bufferReaderclose();

fileReaderclose();

}

catch (Exception e)

{

eprintStackTrace();

}

return contenttoString();

}

/

覆盖文件的内容

/

public static boolean setFileCnt(String filePath, String newContent)

{

try

{

File file = new File(filePath);

if (!fileexists()) filecreateNewFile();

PrintWriter out = new PrintWriter(new FileWriter(file));

outprint(newContent);

outclose();

return true;

}

catch (IOException e)

{

Systemoutprintln("update template false:" + etoString());

return false;

}

}

}

----------------------------------------

String temp=FileUtilgetFileCnt("D:/work/xmsc/jsp/web/public/webcounttxt")trim();

BufferedReader是缓存流的一个类。

缓存流是计算机领域中输入输出流的一种常见形式。包括BufferedInputStream/ BufferedOutputStream 类和BufferedReader/ BufferedWriter类。

BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。缓存流是过滤流,在创建具体流时需要给出一个InputStream / OutputStream 类型的流座位前端流,并可以指明缓冲区的大小。

扩展资料:

输入输出原理:

BufferedReader和BufferedWriter类各拥有8192个字符的缓冲区。当BufferedReader在读取文本文件时,会先尽量从文件中读入字符数据并放满缓冲区,而之后若使用read()方法,会先从缓冲区中进行读取。

如果缓冲区数据不足,才会再从文件中读取,使用BufferedWriter时,写入的数据并不会先输出到目的地,而是先存储至缓冲区中。如果缓冲区中的数据满了,才会一次对目的地进行写出。

BufferedReader字符流以字符为单位传输数据的流,输入流程序可以从中读取数据的流。

参考资料来源:百度百科-缓存流

参考资料来源:百度百科-javaio

以上就是关于怎么用java读取txt文件,我的文件中是字母为什么读出的是数字全部的内容,包括:怎么用java读取txt文件,我的文件中是字母为什么读出的是数字、请问JAVA中的BufferedReader是什么意思、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址:https://www.54852.com/web/10097368.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存