Java读取txt文件和写入txt文件-多种方法
程序员文章站
2022-04-28 13:25:14
...
读取:
第一种:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import org.apache.commons.io.FileUtils;
public class ReadTxt2 {
public static void main(String[] args) throws IOException {
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("test.txt")));
// 使用字符串换行作为分隔符
s.useDelimiter("/n");
while (s.hasNext()) {
// System.out.println(s.next());
// String keyWord = URLDecoder.decode(s.next(), "utf8");
// System.out.println(keyWord);
}
}
}
}
第二种:
File file = new File("testurl.txt");
System.out.println("testurl.txt");
InputStreamReader reader = new InputStreamReader(
new FileInputStream(file));
BufferedReader br = new BufferedReader(reader);
String stringLine;
while ((stringLine=br.readLine())!=null) {
System.out.println(stringLine);
}
写入:
//import org.apache.commons.io.FileUtils;
FileUtils.write(new File("d:/textContent.html"),"写入内容", "gbk");
上一篇: 红灯,那我是不是得停啊!
下一篇: 谷歌计划提前关闭Google+