-
2021-11-23 16:32:15
1.使用FileInputStream实现读取txt文件内容:
2.使用FileOutputStream实现写入txt文件内容:
复制代码
package cn.xiaobing.util;import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;public class ReadTxt {
/**传入txt路径读取txt文件
* @param txtPath
* @return 返回读取到的内容
*/
public static String readTxt(String txtPath) {
File file = new File(txtPath);
if(file.isFile() && file.exists()){
try {
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);StringBuffer sb = new StringBuffer(); String text = null; while((text = bufferedReader.readLine()) != null){ sb.append(text); } return sb.toString(); } catch (Exception e) { e.printStackTrace(); } } return null; } /**使用FileOutputStream来写入txt文件 * @param txtPath txt文件路径 * @param content 需要写入的文本 */ public static void writeTxt(String txtPath,String content){ FileOutputStream fileOutputStream = null; File file = new File(txtPath); try { if(file.exists()){ //判断文件是否存在,如果不存在就新建一个txt file.createNewFile(); } fileOutputStream = new FileOutputStream(file); fileOutputStream.write(content.getBytes()); fileOutputStream.flush(); fileOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } }
}
复制代码
3.验证代码//验证方法:先写入文件后读取打印如下: public static void main(String[] args) { writeTxt("D:/yzm/result1.txt", "测试写入txt文件内容"); String str = readTxt("D:/yzm/result1.txt"); System.out.println(str); }
控制台输出:
更多相关内容 -
Java读取txt文件和写入txt文件的简单实例
2020-09-01 07:06:42下面小编就为大家带来一篇Java读取txt文件和写入txt文件的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧 -
Java读写txt文件
2021-02-12 10:05:481、Java读取txt文件1.1、使用FileInputStream:public static String readFile(File file, String charset){//设置默认编码if(charset == null){charset = "UTF-8";}if(file.isFile() && file.exists()){try...1、Java读取txt文件
1.1、使用FileInputStream:
public static String readFile(File file, String charset){
//设置默认编码
if(charset == null){
charset = "UTF-8";
}
if(file.isFile() && file.exists()){
try {
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charset);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer sb = new StringBuffer();
String text = null;
while((text = bufferedReader.readLine()) != null){
sb.append(text);
}
return sb.toString();
} catch (Exception e) {
// TODO: handle exception
}
}
return null;
}
最后此函数将会返回读取到的内容。当然,也可以在读取的过程中进行逐行处理,不必要一次性读出再进行处理。
而且,bufferedReader还有read方法,满足更多的需要。下去我自己可以试一试,再补充。
2、Java写入txt文件
2.1、使用FileWriter方式:
/**
* 以FileWriter方式写入txt文件。
* @param File file:要写入的文件
* @param String content: 要写入的内容
* @param String charset:要写入内容的编码方式
*/
public static void writeToFile1(){
try {
String content = "测试使用字符串";
File file = new File("./File/test1.txt");
if(file.exists()){
FileWriter fw = new FileWriter(file,false);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close(); fw.close();
System.out.println("test1 done!");
}
} catch (Exception e) {
// TODO: handle exception
}
}
这种方式简单方便,代码简单。顺带一提的是:上述代码是清空文件重写,要想追加写入,则将FileWriter构造函数中第二个参数变为true。
2.2、文件不存在时候,主动创建文件。
public static void writeToFile2(){
try {
String content = "测试使用字符串";
File file = new File("./File/test2.txt");
//文件不存在时候,主动穿件文件。
if(!file.exists()){
file.createNewFile();
}
FileWriter fw = new FileWriter(file,false);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close(); fw.close();
System.out.println("test2 done!");
} catch (Exception e) {
// TODO: handle exception
}
}
关键性的话语在于file.createNewFile();
2.3、使用FileOutputStream来写入txt文件。
public static void writeToFile3(){
String content = "测试使用字符串";
FileOutputStream fileOutputStream = null;
File file = new File("./File/test3.txt");
try {
if(file.exists()){
file.createNewFile();
}
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(content.getBytes());
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("test3 done");
}
使用输出流的方式写入文件,要将txt文本转换为bytes写入。
3、总结
1、对于写入文件路径的问题,我发现文件路径是以本项目为根目录的,也就是说:写文件路径的时候,要么写成绝对路径,防止错误;要么 以本项目为根目录写相对路径。
举一个例子:
|project/
|----src/
| |----test.java
|----File
| |----test1.txt
| |----test2.txt
| |----test3.txt
要想访问到test1.txt,路径要写作绝对路径,或者相对路径:./File/test1.txt
2、对于输入输出流读写文件,操作完成后要关闭流。
以后再进行补充学习,暂时就这样记录下来。
-
java追加写入txt文件的方法总结
2020-08-25 02:41:16在本篇文章里我们给大家整理了关于java如何追加写入txt文件的方法和代码,需要的朋友们可以参考下。 -
java读取写入txt文件
2016-06-16 11:45:58java利用io技术创建文件夹、读取txt文件、写入txt文件(覆盖、不覆盖均有) -
java读写文件(txt)
2017-08-22 14:55:37java中通过cmd指令输入读写文件 -
java读写txt 文件-代码实例
2022-05-08 20:41:47java读写txt 文件-代码实例
源数据:5306行*15列public class WindLoad01 { public static void main(String[] args) throws Exception { File file1 = new File("C:/Users/zzyuan/Desktop/test.txt"); //存取数据 double[][] map = new double[5306][15]; int n = map.length; int m = map[0].length; BufferedReader br = new BufferedReader(new FileReader(file1)); String[] line = null; String s = ""; String str = ""; int index = 0; while ((s = br.readLine()) != null){ line = s.split("\\s+"); for(int j = 0 ; j < 15 ; j++){ map[index][j] = Double.parseDouble(line[j]) - Double.parseDouble(line[j+15]); map[index][j] *= 31; str = String.format("%.2f",map[index][j]); map[index][j] = Double.parseDouble(str); } index++; } File file2 = new File("C:/Users/zzyuan/Desktop/out.txt"); BufferedWriter wr = new BufferedWriter(new FileWriter(file2)); for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[0].length; j++) { wr.write(map[i][j] + "\t"); } wr.write("\r\n"); } //关闭数据流 br.close(); wr.close(); } }
结果:
-
Java读写txt文件时防止中文乱码问题出现的方法介绍
2020-09-03 00:01:33主要介绍了Java读写txt文件时防止中文乱码问题出现的方法,同时需要注意系统默认的文本保存编码的设置,需要的朋友可以参考下 -
java读写txt文件的方法
2021-03-22 20:43:25java读写txt文件的方法发布时间:2020-06-26 15:54:02来源:亿速云阅读:111作者:Leah本篇文章为大家展示了java读写txt文件的方法,代码简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能...java读写txt文件的方法
发布时间:2020-06-26 15:54:02
来源:亿速云
阅读:111
作者:Leah
本篇文章为大家展示了java读写txt文件的方法,代码简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
1、使用FileInputStream实现读取txt文件内容
2、使用FileOutputStream实现写入txt文件内容package cn.xiaobing.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
public class ReadTxt {
/**传入txt路径读取txt文件
* @param txtPath
* @return 返回读取到的内容
*/
public static String readTxt(String txtPath) {
File file = new File(txtPath);
if(file.isFile() && file.exists()){
try {
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer sb = new StringBuffer();
String text = null;
while((text = bufferedReader.readLine()) != null){
sb.append(text);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**使用FileOutputStream来写入txt文件
* @param txtPath txt文件路径
* @param content 需要写入的文本
*/
public static void writeTxt(String txtPath,String content){
FileOutputStream fileOutputStream = null;
File file = new File(txtPath);
try {
if(file.exists()){
//判断文件是否存在,如果不存在就新建一个txt
file.createNewFile();
}
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(content.getBytes());
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.验证代码//验证方法:先写入文件后读取打印如下:
public static void main(String[] args) {
writeTxt("D:/yzm/result1.txt", "测试写入txt文件内容");
String str = readTxt("D:/yzm/result1.txt");
System.out.println(str);
}
说明:
1、FileInputStream
FileInputStream是Java语言中抽象类InputStream用来具体实现类的创建对象。FileInputStream可以从文件系统中的某个文件中获得输入字节,获取的文件可用性取决于主机环境。
FileInputStream的构造方法需要指定文件的来源,通过打开一个到实际文件的连接来创建一个FileInputStream,该文件通过文件系统中的 File 对象 file 指定。
2、FileOutputStream
FileOutputStream,意为文件输出流,是用于将数据写入File或 FileDescriptor的输出流。
FileOutputStream 用于写入诸如图像数据之类的原始字节的流。要写入字符流,请考虑使用 FileWriter。
上述内容就是java读写txt文件的方法,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
-
Java写入txt文件内容
2022-03-21 16:18:49Java写入数据进txt文件,需求:多条数据追加进文件,且需要处理中文编码问题。 以下代码只能处理向文件添加数据的功能,但是会覆盖掉之前的数据 import java.io.File; import java.io.FileOutputStream; import ... -
Java 读写txt文件
2021-03-03 21:05:192.把内容 写入txt文件 import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.... -
java读写文件(txt)
2017-09-04 15:41:28java读取文件内容 转换成字符串 正则匹配获取所有手机号 并保存到另一文件中 文件可以是txt excel word 文件等。 -
java读写txt文件
2021-03-15 04:09:17import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOExc... -
Java读取txt文件和覆盖写入txt文件和追加写入txt
2021-03-06 22:03:52//创建文件public static void createFile(File filename) {try {if(!filename.exists()) {filename.createNewFile();}}catch (Exception e) {// TODO: handle exceptione...}}//写入txt 内容不被覆盖 追加写入publi... -
java实现读取txt文件和将内容写入txt文件
2022-04-23 20:22:231.java读取txt和写入txt文件 package com.ztesoft.TestClusterHHHT; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.... -
java 写入txt文件的中文乱码問題
2021-02-12 18:34:33读取文件的时候如果是用的read方法(字节流),碰到中文输出就是乱码,然后存储的时候设置下编码为GBK或者是UTF-8形式即可,可以有效的解决乱码问题。可以通过BufferedReader 流的形式进行流缓存,之后通过readLine... -
Java读取和写入txt文件
2021-03-15 01:30:271 问题描述对于java的读取和写入txt一直心存疑惑,随着知识的积累,又重新进行学习,对java的文件读写理解更加深刻,在这里将自己的小小经验总结分享给大家。下面是大家了解java流的一个基本框架。 2 问题分析在java... -
Java读写txt文件中文乱码问题的解决
2021-03-06 02:18:06现象:用Java程序读写含中文的txt文件时,读出或写入的内容出现乱码。原因:出现乱码的原因是操作系统和Java程序使用的编码格式不同。中文Windows OS默认使用的是GBK编码,而Eclipse IDE的Encode默认使用的是UTF-8... -
java创建txt文件并存入内容
2020-08-27 01:43:39主要为大家详细介绍了java创建txt文件并存入内容,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
java写入txt文件 想要修改txt文件每一行的第一个数字 加一就好
2021-03-03 12:33:16展开全部写了一个简易的方案,但32313133353236313431303231363533e4b893e5b19e...)按行读取文件,然后按照空格分组,对第一个数字加1,然后写入新的文件。publicstaticvoidwriteFile(){BufferedReaderreader=n... -
java读写Txt文档
2021-03-17 18:41:36java读写Txt文档public class MyFile { private static String path = "D:/"; private static String filenameTemp; /** * 创建文件 * * @throws IOException */ public static boolean creatTxtFile(String name) .... -
安卓端Java写入txt
2021-01-05 11:16:54安卓端Java写入txt -
Java读写TXT文件(JSON格式)
2020-12-06 15:52:39Java读写TXT文件(JSON格式) 读文件代码: /* 读文件(json) */ public static Map<String, Object> parseFile(String path) { try { File file = new File(path); if (file.isFile() && ...