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 以下是输入的代 ...
随机推荐
- 配置IIS5.5/6.0 支持 Silverlight
在安装完Silverlight1.1 Alpha后,要使自己的IIS服务器支持Silverlight的浏览还需要配置一下IIS网站的 Http头->MIME映射添加内容如下:扩展名 ...
- .NET平台上的编译器不完全列表(转别)
http://www.cnblogs.com/william_fire/archive/2005/05/15/155800.html最近因为开发需要,要研究一下.NET上基于C#扩展的编译器实现的框架 ...
- 15个 MySQL 基础面试题,DBA 们准备好了吗?
此前我们已经有发表过Linux 面试基础问答之一.二和三共3篇文章,获得读者的好评,同时我们得到反馈,有些读者希望这种交互式学习方法能够做得更加灵活.心动不如行动,我们这就为您奉上 15个 MySQL ...
- spring boot 集成mybatis使用logback打印并保存日志信息
spring boot 打印执行的sql语句 最近在学习spring boot 整合了Mybatis和druid之后总感觉少点什么东西,看了下在别的项目上用的框架,发现自己整合的东西不打印sql语句, ...
- Codeforces Round #618 (Div. 2)-B. Assigning to Classes
Reminder: the median of the array [a1,a2,-,a2k+1] of odd number of elements is defined as follows: l ...
- 数学--数论--随机算法--Pollard Rho 大数分解算法(纯模板带输出)
ACM常用模板合集 #include <bits/stdc++.h> using namespace std; typedef long long ll; ll pr; ll pmod(l ...
- 2019 Multi-University Training Contest 10 I Block Breaker
Problem Description Given a rectangle frame of size n×m. Initially, the frame is strewn with n×m squ ...
- 安卓commandlinetools-win-6200805_latest配置
JDK:1.8.0_251 系统:win10 64bit 问题1 官网下载commandlinetools,解压运行报错 解决方法 打开sdkmanager.bat,修改第17行为set DEFAUL ...
- PHP循环引用会遇到的坑
今天遇到这样一个问题: 如果foreach循环一个数组,引用去对它的元素做一些操作,会有什么问题吗? 比如 [1, 2, 3],foreach循环的时候,引用给每个元素 * 2,再去foreach输出 ...
- Android下拉刷新SwipeRefreshLayout简单用法
之前一直都想用下拉刷新,感觉上是庞大的工程,所以搁置了.现在学习了一下其实真的超级简单. 看了<第一行代码>以及 https://www.jianshu.com/p/3c402a9e4b7 ...