import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass
{
public static void main(String[] args)
{
}
/**
* 使用FileWriter类写文本文件
*/
public static void writeMethod1()
{
String fileName="C:\kuka.txt";
try
{
//使用这个构造函数时,如果存在kuka.txt文件,
//则先把这个文件给删除掉,然后创建新的kuka.txt
FileWriter writer=new FileWriter(fileName);
writer.write("Hello Kuka:
");
writer.write(" My name is coolszy!
");
writer.write(" I like you and miss you。");
writer.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 使用FileWriter类往文本文件中追加信息
*/
public static void writeMethod2()
{
String fileName="C:\kuka.txt";
try
{
//使用这个构造函数时,如果存在kuka.txt文件,
//则直接往kuka.txt中追加字符串
FileWriter writer=new FileWriter(fileName,true);
SimpleDateFormat format=new SimpleDateFormat();
String time=format.format(new Date());
writer.write("
"+time);
writer.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
//注意:上面的例子由于写入的文本很少,使用FileWrite类就可以了。但如果需要写入的
//内容很多,就应该使用更为高效的缓冲器流类BufferedWriter。
/**
* 使用BufferedWriter类写文本文件
*/
public static void writeMethod3()
{
String fileName="C:/kuka.txt";
try
{
BufferedWriter out=new BufferedWriter(new FileWriter(fileName));
out.write("Hello Kuka:");
out.newLine(); //注意
不一定在各种计算机上都能产生换行的效果
out.write(" My name is coolszy!
");
out.write(" I like you and miss you。");
out.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 使用FileReader类读文本文件
*/
public static void readMethod1()
{
String fileName="C:/kuka.txt";
int c=0;
try
{
FileReader reader=new FileReader(fileName);
c=reader.read();
while(c!=-1)
{
System.out.print((char)c);
c=reader.read();
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 使用BufferedReader类读文本文件
*/
public static void readMethod2()
{
String fileName="c:/kuka.txt";
String line="";
try
{
BufferedReader in=new BufferedReader(new FileReader(fileName));
line=in.readLine();
while (line!=null)
{
System.out.println(line);
line=in.readLine();
}
in.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
============================================================
按行读取
//读取文件(字符流)
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("d:\1.txt"),"GBK"));#这里主要是涉及中文
//BufferedReader in = new BufferedReader(new FileReader("d:\1.txt")));
//写入相应的文件
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\2.txt"),"GBK"));
//BufferedWriter out = new BufferedWriter(new FileWriter("d:\2.txt"));
//读取数据
//循环取出数据
String str = null;
while ((str = in.readLine()) != null) {
System.out.println(str);
//写入相关文件
out.write(str);
out.newLine();
}
//清楚缓存
out.flush();
//关闭流
in.close();
out.close();
============================================================
按行读取
package com.shaonaiyi.local;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
/**
* Java实现本地读文件
*/
public class ReadFile {
public static void main(String[] args) throws Exception{
String path = "/Users/shaonaiyi/datas/tmp/hello.txt";
//win系统
// String path = "c:\hello.txt";
FileInputStream fileInputStream = new FileInputStream(path);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
fileInputStream.close();
}
}
============================================================
REF
https://blog.csdn.net/jiangxinyu/article/details/7885518
https://www.cnblogs.com/qianbi/p/3378466.html
https://blog.csdn.net/shaock2018/article/details/90607893
https://www.cnblogs.com/zhuocheng/articles/2285290.html