写了读取文件写文件的小例子,Mark以下,以后自己好找。
发现了新的方法,File.createNewFile();创建指定名称的空文件
File file=new File("D:/爱你/希望/","工作.txt");
if(!file.exists())
file.getParentFile().mkdirs();
file.createNewFile();
public class zijie_in_output {
/**
* 字节流的方法输入输出到一个文件或者控制台上 OutputStream的子类FileOutputStream
* InputStream的子类FileInputStream Scanner是PrintWriter的子类
*/
private String str;// 存储用户从控制台输入的数据
void sys_input() {// 从控制台读取一句话写入文件中
Scanner scanner = new Scanner(System.in);// 获取用户的输入类
System.out.println("请输入存到D盘output文件中的字符串");
if (scanner.hasNextLine())// hasNext()检测是否有数据,遇到enter键结束返回
str = scanner.nextLine();// nextLine()会输出空格
scanner.close();
}
void output_file() {// 把读到的话写入到文件中,文件的位置:D盘的output文件夹下
OutputStream output;
/*
* File类里面有两个方法可以实现: 一个是mkdir():创建此抽象路径名指定的目录。 另外一个是 mkdirs(),mkdirs()能创建不存在的两层文件夹,mkdir()一层
* 创建此抽象路径名指定的目录,包括所有必需但不存在的父目录
*/
// 创建文件夹对象
File file = new File("D:/C瓜哥");// 只能创建文件夹不能写文件名
if (!file.exists()) {// 判断文件是否存在
file.mkdir();// 正式创建了一个文件夹
}
try {
output = new FileOutputStream(file + "/girl.txt");// 创建一个输出字节流的对象,也可以直接包括创建文件夹new
// FileOutputStream("D:/output.txt");
byte b[] = str.getBytes();// 字符串转化为字节类型存储到字节数组中
try {
output.write("".getBytes());// 清空文件里面原来的内容
output.write(b);// 往文件中以字节的方式写入字符串
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void read_sys() {// 将D盘文件下output的内容读出到控制台上
try {
InputStream input = new FileInputStream("D:/C瓜哥/girl.txt");
try {
int size = input.available();// 求input一次性能读出多少个字节
byte b[] = new byte[size];
input.read(b);// read(byte[])读取字节序列,放到字节数组b里,因为汉字是3个字节或者2个字节都有可能,转化为char会乱码,
System.out.println(b);
System.out.println((char) input.read(b));
System.out.print(new String(b));// 容易出现乱码,尤其是中文都乱码,所以一般引入字符流,InputStreamReader
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void read_file() {// 将文件的内容读出到另一个文件中
// 源文件地址:D:/output.txt
// 读出到的文件地址:E:/guagua/girl.txt
// 先读出来
InputStream input_file;
try {
input_file = new FileInputStream("D:\\C瓜哥\\girl.txt");// 两道杠,第一道杠是转义字符
try {
int size = input_file.available();
byte[] b = new byte[size];
input_file.read(b);
// 再写进另个文件里
File file = new File("E:\\guagua");
if (!file.exists())
file.mkdirs();
OutputStream out_file = new FileOutputStream(file
+ "\\girls.txt");
out_file.write(b);
input_file.close();
out_file.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
zijie_in_output zz = new zijie_in_output();
zz.sys_input();
zz.output_file();
zz.read_sys();
zz.read_file();
}
}
public class zifu_in_output {
/**
* 字符流的方法输入输出文件到控制台或者另一个文件上
*/
protected void readFileToSys() {// 读取文件输出到控制台
String q = "";
try {
FileReader fr = new FileReader("D:\\龙炎\\大狗.txt");// 创建一个fileReader对象
BufferedReader buff = new BufferedReader(fr);// BufferedReader字符流缓冲对象效率高
String t = null;
try {
while ((t = buff.readLine()) != null)
// 读取以换行符结尾的一行,字符流结尾是null
q = q.concat(t);// concat结果是返回值才是连接值,q和t的值没变
System.out.println(q);
buff.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void readFileToFile() {// 读取文件输出到另一个文件
/**
* 目标文件:E:/花花/猪猪.txt
*/
// 先读取
String ss = "";
String str = null;
try {
FileReader file = new FileReader("D:\\龙炎\\大狗.txt");
BufferedReader br = new BufferedReader(file);
try {
while ((str = br.readLine()) != null)
// 读取一行,末尾是null
ss = ss.concat(str);
// 写进文件里
File f = new File("e:\\花花");
if (!f.exists())
f.mkdirs();
FileWriter fw = new FileWriter(f + "\\猪猪.txt", true);// 是否是追加的形式?
fw.write(ss);// 写进文件里
fw.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void ReadSysToFile() {// 读取控制台的写进文件里面
String str = null;
Scanner scanner = new Scanner(System.in);
System.out.println("输入存到D:\\龙炎\\大狗的数据");
if (scanner.hasNextLine())
str = scanner.nextLine();
System.out.println(str);// 输出到控制台
scanner.close();
// 写进文件里
try {
File file = new File("d:\\龙炎");
if (!file.exists())
file.mkdirs();
FileWriter fw = new FileWriter(file + "\\大狗.txt", false);// 容易出错,如果是有文件夹的都出错,要新建文件夹
fw.write(str);
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
zifu_in_output zz = new zifu_in_output();
zz.ReadSysToFile();
zz.readFileToSys();
zz.readFileToFile();
}
}