Java——读取和写入txt文件
package com.java.test.a; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException; public class ReadAndWriteTXT {
public static String resolveCode(String path) throws Exception {
InputStream inputStream = new FileInputStream(path);
byte[] head = new byte[3];
inputStream.read(head);
String code = "gb2312"; //或GBK
if (head[0] == -1 && head[1] == -2 )
code = "UTF-16";
else if (head[0] == -2 && head[1] == -1 )
code = "Unicode";
else if(head[0]==-17 && head[1]==-69 && head[2] ==-65)
code = "UTF-8"; inputStream.close();
return code;
} public static void main(String[] args) { /**读取txt文件*/
String pathname = "D:/test.txt"; // 绝对路径或相对路径都可以,这里是绝对路径
String code = "";
try {
code = resolveCode(pathname);//获取txt文档编码
} catch (Exception e1) {
e1.printStackTrace();
}
File filename = new File(pathname); //读取文件
InputStreamReader reader = null;
BufferedReader br = null;
try {
try {
reader = new InputStreamReader(new FileInputStream(filename),code);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} //输入流
br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
String line = "";
while ((line = br.readLine()) != null) {
// line = br.readLine(); // 一次读入一行数据
System.out.println(line);
}
} catch (IOException e) {
System.out.println("文件读取错误");
} //在写入文件时,若没有找到目标文件,则会创建一个
/**写入txt文件*/
File writename = new File(".\\file\\output.txt"); //相对路径,以项目文件为根路径:Test/file/output.txt
BufferedWriter out = null;
try {
writename.createNewFile(); // 创建新文件
out = new BufferedWriter(new FileWriter(writename));
out.write("写入文件测试\r\n第二行\r\n第三行\r\nnqwertyuiop\r\n1234567890"); // \r\n即为换行
out.flush(); // 把缓存区内容压入文件
out.close(); // 关闭输出
} catch (IOException e) {
System.out.println("文件输入错误");
e.printStackTrace();
} } }
相对路径:
在使用相对路径读取和写入文件时,相对项目根目录Test进行读取,例如:./file/output.txt,完整路径为D:/workspace/Test/file/output.txt
Java——读取和写入txt文件的更多相关文章
- java读取数据写入txt文件并将读取txt文件写入另外一个表
package com.xsw.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.F ...
- java读取和写入txt文件
package com.yinghuo.testDES; import java.io.BufferedReader;import java.io.BufferedWriter;import java ...
- Asp.net读取和写入txt文件方法(实例)!
Asp.NET读取和写入txt文件方法(实例)! [程序第一行的引入命名空间文件 - 参考] System; using System.Collections; using System.Config ...
- java读取字符串,生成txt文件
/** * 读取字符串,生成txt 文件 已解决未设置编码时,在项目中直接打开文件,中文乱码问题 * WriteText.writeToText(musicInfo,fileName)直接调用 * * ...
- java读取UTF-8的txt文件发现开头的一个字符问题
今天遇到一个奇葩问题,在读取一个TXT文件时,出现开头多了一个问号(?).如下图: 莫名奇妙的多了一个.最后通过网上资料,知道在Java中,class文件采用utf8的编码方式,JVM运行时采用utf ...
- C# 读取和写入txt文件
读取: 1.使用StreamReader读取文件,然后一行一行的输出 StreamReader sr = new StreamReader(path,Encoding.Default); String ...
- java如何追加写入txt文件
java中,对文件进行追加内容操作的三种方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.io.BufferedWriter; import ...
- JAVA读取和写入properties文件
1.读取 Properties prop = new Properties(); try { //这个getResourceAsStream方法就是把文件转为inputStream的方式 prop.l ...
- java 读取并且显示 txt 文件
系统:mac os x 10.9 eclipse 在eclipse 中建立一个project, 命名为Cin_txt, Cin_txt的内容 test wang hello world 以下是输入的代 ...
随机推荐
- Spark 源码系列(六)Shuffle 的过程解析
Spark 大会上,所有的演讲嘉宾都认为 shuffle 是最影响性能的地方,但是又无可奈何.之前去百度面试 hadoop 的时候,也被问到了这个问题,直接回答了不知道. 这篇文章主要是沿着下面几个问 ...
- 自动驾驶汽车数据不再封闭,Uber 开源新的数据可视化系统
日前,Uber 开源了基于 web 的自动驾驶可视化系统(AVS),称该系统为自动驾驶行业带来理解和共享数据的新方式.AVS 由Uber旗下负责自动驾驶汽车研发的技术事业群(ATG)开发,目前该系统已 ...
- element-ui中cascader同时获取label和value值
关于elementUI中cascader选中值后,能获取value或者label,但不能同时获value和label,这一问题,琢磨出了这么个办法.以新增和编辑城市为例,type: 1 编辑,type ...
- FreeRTOS-Qemu 实现三任务同步通信机制以及API信息
1. 本次作业的考察要点: 作业地址:github.com/HustWolfzzb-Git/GCC/GDB/QEMU等工具的使用.FreeRTOS多任务同步和通信机制的掌握. 2. 编程作业: 在gi ...
- 2019 ICPC 南京网络赛 H-Holy Grail
As the current heir of a wizarding family with a long history,unfortunately, you find yourself force ...
- predixy源码学习
Predixy是一个代理,代理本质上就是用来转发请求的.其主要功能就是接收客户端的请求,然后把客户端请求转发给redis服务端,在redis服务端处理完消息请求后,接收它的响应,并把这个响应返回给客户 ...
- Linux查看redis占用内存的方法
redis-cli auth 密码info # Memory used_memory:13490096 //数据占用了多少内存(字节) used_memory_human:12.87M //数据占用了 ...
- Java λ表达式
目录 Java Lambda表达式 1. 函数式编程思想 2. 体验Lambda的更优写法 3. Lambda表达式的标准格式 4. 练习:使用Lambda表达式(无参无返回) 5. 练习:使用Lam ...
- 2018面向对象程序设计(Java)学习进度条
周次 (阅读/编写)代码行数 发布博客量/评论他人博客数量 课堂/课余学习时间(小时) 最满意的编程任务 第一周 200/30 1/0 6/3 九九乘法表 第二周 200/30 1/0 4/3 实验2 ...
- Python 为什么抛弃累赘的花括号,使用缩进来划分代码块?
大家好,这是"Python为什么"系列节目的文字稿(文末有观看地址). 本期话题:Python 为什么使用缩进来划分代码块,而不像其它语言使用花括号 {} 或者 "end ...