Java file剩余部分总结Java file剩余部分总结

Java file剩余部分总结

Java file剩余部分总结
Java file剩余部分总结

Java file剩余部分总结

package file1;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterDemo {

	public static void main(String[] args) {
		File file = new File("test.txt");
		try {
			
			//用FileWriter写入使会将写入内容写入缓冲区中,若不关闭,则无法写入文件,但可以用flush函数解决这一问题
			//调用close函数是则会自动先执行flush函数
			FileWriter fw = new FileWriter(file);
			fw.write("现在我们来进行文件字符流写入操作");
			fw.write("\r\n");
			fw.write("你看明白了吗");
			fw.append("hello world");
			fw.flush();
			//fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}

package file1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FileOuputStreamdemo {

	public static void main(String[] args) {
		File file = new File("test.txt");
		OutputStream out = null;
		String str ="hello world";
		byte[] b = str.getBytes();
		try {
			out = new FileOutputStream(file);
			out.write(b);
			out.close();
			System.out.println("文件写入成功");
		} catch (FileNotFoundException e) {
		System.out.println("文件找不到");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("文件写入失败");
			e.printStackTrace();
		}
	}

}

package file1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class bufferedreaderdemo {

	public static void main(String[] args) {
		// 读取9*9
		File file = new File("test1.txt");
		try {
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String str=null;
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			while(str!=null) {
				System.out.println(str);
				str = br.readLine();
			} 
			System.out.println("文件读取成功");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

package file1;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class bufferedwriterdemo {

	public static void main(String[] args) {
		// 使用bufferedwriter向文件poem。txt中写入一首诗
		//"碧玉妆成一树高"" 万条垂下绿丝绦""不知细叶谁裁出""二月春风似剪刀"
		//要求文件中每一句一行,且是在内存中使用字符串数组储存
		String[] poems = {"碧玉妆成一树高"," 万条垂下绿丝绦","不知细叶谁裁出","二月春风似剪刀"};
		File file = new File("poem.txt");
		FileWriter fw;
		try {
			fw = new FileWriter(file);
			BufferedWriter bw = new BufferedWriter(fw);
			for(String s:poems) {
				bw.write(s);
				bw.newLine();
			}
			bw.close();
			fw.close();
			System.out.println("输入成功");
		} catch (IOException e) {	
			e.printStackTrace();
		}
		
	}

}

Java file剩余部分总结
Java file剩余部分总结

package file1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class bufferedreaderdemo2 {

	public static void main(String[] args) {
		//System.in是inputstream,所以如果要用字符流输入则需要转换
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			String str = br.readLine();
			while(str != null)
			{
				System.out.println(str);
				str = br.readLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

对象流
1.对象序列化和反序列化

把对象转换为字节序列的过程成为对象的序列化,Serialization
把字节序列恢复为对象的过程成为对象的反序列化
Deserializtion

对象序列化的用途:

1.把对象字节序列永久保存到硬盘上
2.在网络上传送对象的字节序列
3.通过序列化在进程间传递对象

package Object;

import java.io.Serializable;

public class Person implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public String name;
	public int age;
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "person [姓名="+name +",年龄="+age+"]";
	}
}

package Object;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

public class ObjectInputStreamdemo {

	public static void main(String[] args) {
		File file = new File("person.txt");
		FileInputStream fis;
		try {
			fis = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fis);
			Person person = (Person)ois.readObject();
			System.out.println(person);
			ois.close();
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

}
package Object;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ObjectOutputSteamdemo {

	public static void main(String[] args) {
		File file = new File("person.txt");
		FileOutputStream fo;
		try {
			fo = new FileOutputStream(file);
			ObjectOutputStream oj = new ObjectOutputStream(fo);
			Person person = new Person("kate",18);
			oj.writeObject(person);
			oj.close();
			fo.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

transient关键字:

用来表示一个域不是对象序列化的一部分.

实例:
1.将序列化和反序列化方法定义在同一个类中;
2.在Person类中增加static的county属性;
3.使用transient关键字修饰age属性

结论:

对象中的transient和static类型的成员变量不会被读取和写入

package Object;

import java.io.Serializable;

public class Person implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public String name;
	transient int age;
	static String country=null;
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "person [姓名="+name +",年龄="+age+",城市="+country+"]";
	}
}

package Object;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

public class ObjectInputStreamdemo {

	public static void main(String[] args) {
		File file = new File("person.txt");
		FileInputStream fis;
		try {
			fis = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fis);
			Person person = (Person)ois.readObject();
			System.out.println(person);
			ois.close();
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

}

package Object;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataStreamdemo {

	public static void main(String[] args) {
		Person[] persons = new Person[] {new Person("kate",18),new Person("lucy",18)};
		File file  = new File("persons.txt");
		try {
			DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
			for(Person s:persons) {
				dos.writeUTF(s.name);
				dos.writeInt(s.age);
			}
			System.out.println("输入成功");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//数据读取
		try {
			DataInputStream dis = new DataInputStream(new FileInputStream(file));
			//将读取出来的内容赋值给新的person数组
			Person[] newPersons = new Person[persons.length];
			for(int i=0;i<persons.length;i++) {
			String str=dis.readUTF();
			int age = dis.readInt();
			System.out.println(str);
			System.out.println(age);
			Person person = new Person(str, age);
			newPersons[i] = person;
			}
			//读取内容的顺序应当和写入内容的顺序保持一致
			dis.close();
			
			for(Person s:newPersons) {
				System.out.println(s.name);
				System.out.println(s.age);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}
package Object;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Datastreamdemo2 {

	public static void main(String[] args) {
		String[] name = {"帽子","皮带","眼睛"};
		double[] prices = {20.5,85.5,15.6};
		int[] nums = {10,20,5};
		
		File file = new File("product.txt");
		try {
			DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
			for(int i=0;i<name.length;i++) {
				dos.writeUTF(name[i]);
				dos.writeDouble(prices[i]);
				dos.writeInt(nums[i]);
			}
			System.out.println("输入成功");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		//读取商品信息
		try {
			DataInputStream dis = new DataInputStream(new FileInputStream(file));
			for(int i=0;i<name.length;i++) {
				String str=dis.readUTF();
				double price = dis.readDouble();
				int num = dis.readInt();
				System.out.println(str+","+price+","+num);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}

Java file剩余部分总结

package file1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

public class 标准输入输出流 {

	public static void main(String[] args) {
		File file = new File("file1.txt");
		PrintStream ps;
		try {
			ps = new PrintStream(file);
			ps.print("abcdef");
			ps.println();
			ps.write("abcdef".getBytes());//将字符串变成byte数组
			ps.write(97);
			ps.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
package file1;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class 文件复制 {

	public static void main(String[] args) {
		FileReader fr;
		try {
			//将文件中的内容读取出来
			fr = new FileReader("poem.txt");
			BufferedReader br = new BufferedReader(fr);
			
			//构建复制输出的标准输出流
			
			PrintWriter ps = new PrintWriter(new FileWriter("target.txt"));
			String str = br.readLine();
			while(str!=null) {
				ps.println(str);
				str= br.readLine();
			}
			br.close();
			fr.close();
			ps.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

相关文章:

  • 2021-11-26
  • 2021-07-21
  • 2021-07-25
  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
  • 2021-11-07
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-11
  • 2021-06-24
  • 2021-07-13
  • 2021-07-31
  • 2021-12-04
相关资源
相似解决方案