读取输入

通过控制台输入
import java.util.*;

Scanner in = new Scanner(System.in);
String str = in.nextLine();  //输入下一行内容,可包含空格
int a = in.nextInt();  //读整数
String word = in.next(); //读一个单词
double b = in.nextDouble();  //读整数或浮点数

格式化输出

printf格式化输出

%字符开始的格式说明符用相应参数替换
f为浮点数,s为字符串,c为字符,b为布尔,d为十进制整数,x为十六进制整数,o为八进制整数

System.out.printf("Hello, %s. It's %d o'clock.", name, clock);
System.out.printf("%8.2f", x);  //eg. 33333.33
System.out.printf("%,.2f", x);  //eg. 33,333.33

创建格式化字符串

静态的String.format方法创建字符串,不打印输出

String message = String.format("Hello, %s. It's %d o'clock.", name, clock);

文件输入输出

字节流

InputStream,OutStream为抽象类,使用FileInputStream, FileOutputStream输入输出

public static void main(String[] args) {
	File f1 = new File("f:/test1.txt");
	File f2 = new File("f:/test2.txt");
	try (FileInputStream fis = new FileInputStream(f1);
		FileOutputStream fos = new FileOutputStream(f2)) { 
		// 输入
		byte[] all = new byte[(int) f.length()];
		fis.read(all);
		for (byte b : all) {
			System.out.println(b); // 输出字符对应ASCII码
		}
			
		// 输出
		byte data[] = {65, 66};  // A,B
		fos.write(data);
	} catch (IOException e) {
		e.printStackTrace();
	}
}

字符流

Reader, Writer用于字符形式,使用FileReader,FileWriter读写

public static void main(String[] args) {
	File f1 = new File("f:/test1.txt");
	File f2 = new File("f:/test2.txt");
	try (FileReader fr = new FileReader(f1);
		FileWriter fw = new FileWriter(f2)) {
		// 输入
		char[] all = new char[(int) f.length()];
		fr.read(all);
		for (char b : all) {
			System.out.println(b);
		}
		
		// 输出
		String data = "abcde";
		char[] cs = data.toCharArray();
		fr.write(cs);
	} catch (IOException e) {
		e.printStackTrace();
	}
}

缓存流

字节流和字符流弊端:每次读写都会访问硬盘,读写频率高时,性能表现不佳
缓存流:一次性读较多的数据到缓存中,每次读取都先到缓存访问;写入数据时会先写入缓存区,达到一定量才写入硬盘

BufferedReader BufferedWriter

使用readLine()读文本

BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("c:\\mydir\\1.txt", "GBK")));   //GBK用于中文

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:\\mydir\\2.txt", "GBK")));

String str = null;
while ((str = in.readLine()) != null) {  //读文件
	//写文件
	out.write(str);
	out.newLine();
}

out.flush(); //清除缓存,立即把数据写到硬盘中
in.close();
out.close();

BufferedInputStream BufferedOutputStream

BufferedInputStream in = new BufferedInputStream(new FileInputStream("c:\\1.txt"));
   
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("c:\\2.txt"));

byte[] bytes = new byte[2048];  //一次性取多少字节
int n = -1;
     
while ((n = in.read(bytes,0,bytes.length)) != -1) {    //从数组第0个元素开始存
            
  	String str = new String(bytes,0,n); //转换成字符串
    System.out.println(str);
            
    out.write(bytes, 0, n);   //写文件
 }
out.flush();
in.close();
out.close();

数据流

DataInputStream, DataOutputStream直接进行字符串读写
要用DataInputStream读取文件,这个文件必须是由DataOutputStream写入的

public static void main(String[] args) {
	File f = new File("f:/test.txt");
	// 写入
	try (FileOutputStream fos = new FileOutputStream(f);
		DataOutputStream dos = new DataOutputStream(f)) {
		dos.writeBoolean(true);
		dos.writeInt(300);
		dos.writeUTF("this is maya");
	} catch(IOException e) {
		e.printStackTrace();
	}
	// 读出
	try (FileInputStream fis = new FileInputStream(f);
		DataInputStream dis = new DataInputStream(fis)){
		boolean b = dis.readBoolean();
		int i = dis.readInt();
		String str = dis.readUTF();
		System.out.println("布尔值: " + b);
		System.out.println("整数: " + i);
		System.out.println("字符串: " + str);
	} catch (IOException e) {
		e.printStackTrace();
	}
}

对象流

对象流是将一个对象以流的形式传输给其他介质,如硬盘
对象以流的形式进行传输叫做序列化。对象所对应的类必须实现Serializable接口
想要保存对象到文件或数据库中或想用套接字在网络上传送对象时,需要使用序列化接口

public class Student implements Serializable {
	public String name;
	public long number;
}

public class Test {
	public static void main(String[] args) {
		Student s = new Student();
		s.name = "xiaohong";
		s.number = 10101;
		
		File f = new File("f:/student.txt");
		try ( // 对象输出流
			FileOutputStream fos = new FileOutputStream(f);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
	          // 对象输入流
			FileInputStream fis = new FileInputStream(f);
			ObjectOutputStream ois = new ObjectOutputStream(fis)) {
			// 写入
			oos.writeObject(s);
			// 读出
			Student s1 = (Student) ois.readObject();
			System.out.println(s1.name);
			System.out.println(s1.number);
		} catch (IOException e) {
			e.printStackTrace();
		}
	} 
}

Scanner读取文件

用File对象构造Scanner对象

Scanner in = new Scanner(Paths.get("myfile.txt", "UTF-8"));
Scanner in = new Scanner(Paths.get("c:\\mydir\\myfile.txt", "UTF-8"));  //使用绝对路径

PrintWriter写入文件

构造一个PrintWriter对象(下一次写前会清空)

PrintWriter out = new PrintWriter(""c:\\mydir\\myfile.txt", "UTF-8"");

若文件不存在,创建该文件。
使用print、println和printf命令输出。

追加

以上创建的输出流都会清空文件内容

//构造函数中的第二个参数true表示以追加形式写文件
FileWriter writer = new FileWriter("c:\\mydir\\myfile.txt", true);
writer.write(str);
writer.close();