java 输入输出

未标题-6.jpg

image.png

标准输入输出流

package test;

import java.io.IOException;

public class Hello{
	

	public static void main(String[] args) {
		
		byte[] buffer = new byte[1024];
		try {
			int len = System.in.read(buffer);
			String s = new String(buffer, 0, len);
			System.out.println("读到了:" + len + "字节");
			System.out.println(s);
			System.out.println("s的长度是:" + s.length());
		}catch(IOException e) {
			e.printStackTrace();
		}
		
	}
}

文件流

package test;

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

public class Hello{
	

	public static void main(String[] args) {
		
		byte[] buffer = new byte[10];
		for(int i = 0; i< buffer.length; i++) {
			buffer[i] = (byte)i;
		}
		try {
			FileOutputStream out = new FileOutputStream("test.dat");
			out.write(buffer);
			out.close();
		}catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			
		}
		
	}
}

流过滤器

如果写入的是十进制,实际写入文件的是二进制

package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Hello{
	

	public static void main(String[] args) {
		
		byte[] buffer = new byte[10];
		for(int i = 0; i< buffer.length; i++) {
			buffer[i] = (byte)i;
		}
		try {
			DataOutputStream out = new DataOutputStream(
					new BufferedOutputStream(
						new FileOutputStream("test.dat")));
		
			int i = 123456;
			out.writeInt(i);
			out.close();
			DataInputStream in = new DataInputStream(
					new BufferedInputStream(
							new FileInputStream("test.dat")));
			
			int j = in.readInt();
			System.out.println(j);
			
		}catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			
		}
		
	}
}

文本流

image.png

package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class Hello{
	

	public static void main(String[] args) {
		
		byte[] buffer = new byte[10];
		for(int i = 0; i< buffer.length; i++) {
			buffer[i] = (byte)i;
		}
		try {
			PrintWriter out = new PrintWriter(
				new BufferedWriter(
						new OutputStreamWriter(
								new FileOutputStream("test.txt"))));
				
			int i = 123456;
			out.println(i);
			out.close();
		
		}catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			
		}
		
	}
}

读取文本

image.png
image.png
image.png

package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class Hello{
	

	public static void main(String[] args) {
		
		byte[] buffer = new byte[10];
		for(int i = 0; i< buffer.length; i++) {
			buffer[i] = (byte)i;
		}
		try {
			PrintWriter out = new PrintWriter(
				new BufferedWriter(
						new OutputStreamWriter(
								new FileOutputStream("test.txt"))));
				
			int i = 123456;
			out.println(i);
			out.close();
		
			
			BufferedReader in = new BufferedReader(
					new InputStreamReader(
							new FileInputStream("src/test/Hello.java")));
			/*	
			//指定编码
			BufferedReader in = new BufferedReader(
					new InputStreamReader(
							new FileInputStream("src/test/Hello.java"), "utf8"));
			*/
			String line;
			while((line = in.readLine()) != null) {
				System.out.println(line);
			}
		}catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			
		}
		
	}
}

汉字编码

image.png