欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

I/O 3JAVA IO详解(字符流)

程序员文章站 2024-03-04 15:32:47
...

I/O 3JAVA IO详解(字符流)

I/O 3JAVA IO详解(字符流)

I/O 3JAVA IO详解(字符流)

I/O 3JAVA IO详解(字符流)

I/O 3JAVA IO详解(字符流)

I/O 3JAVA IO详解(字符流)

I/O 3JAVA IO详解(字符流)

I/O 3JAVA IO详解(字符流)

I/O 3JAVA IO详解(字符流)

I/O 3JAVA IO详解(字符流)

I/O 3JAVA IO详解(字符流)

I/O 3JAVA IO详解(字符流)

 

StreamTest

package com.test.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class StreamTest {
	public static void main(String[] args) throws IOException {
		
		FileOutputStream fos = new FileOutputStream("test.txt");
		OutputStreamWriter osw = new OutputStreamWriter(fos);
		BufferedWriter bw = new BufferedWriter(osw); //装饰模式
		
		bw.write("www.google.com");
		bw.write("\n");
		bw.write("www.baidu.com");
		bw.close();
		
		FileInputStream fis = new FileInputStream("test.txt");
		InputStreamReader isr = new InputStreamReader(fis);
		BufferedReader br = new BufferedReader(isr);
		String str = br.readLine();
		while(str != null){
			System.out.println(str);
			str = br.readLine();
		}
	}
}

 

package com.test.io;

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

public class StreamTest2 {
	public static void main(String[] args)throws IOException {
		//System.in表示从标准输入设备上读取输入
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);

		String str = br.readLine();
		while(null != str){
			System.out.println(str);
			str = br.readLine();
		}
		br.close();
	}
}

 

I/O 3JAVA IO详解(字符流)

I/O 3JAVA IO详解(字符流)

 

package com.test.io;

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

public class FildReadTest{
	public static void main(String[] args)throws IOException {
		FileReader fr = new FileReader("test.txt");
		BufferedReader br = new BufferedReader(fr);
		String str = br.readLine();
		while(str != null){
			System.out.println(str);
			str = br.readLine();
		}
		br.close();
	}
}

 

I/O 3JAVA IO详解(字符流)

I/O 3JAVA IO详解(字符流)

 I/O 3JAVA IO详解(字符流)

I/O 3JAVA IO详解(字符流)

I/O 3JAVA IO详解(字符流)

 

package com.test.io;

import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileTest {
	public static void main(String[] args) throws IOException {
		Person person1 = new Person("zhangsan",11,1.68);
		RandomAccessFile raf = new RandomAccessFile("person.txt","rw");
		person1.write(raf);
		
		raf.seek(0);//让读的位置重新回到文件开关
		
		Person person2 = new Person();
		person2.read(raf);
		System.out.println(person2.getName());
		System.out.println(person2.getAge());
		System.out.println(person2.getHeight());
	}
}
class Person{
	private String name;
	private int age;
	private double height;
	
	public Person(){
	}
	
	public Person(String name,int age,double height){
		this.name = name;
		this.age = age;
		this.height = height;
	}
	
	public void write(RandomAccessFile raf) throws IOException{
		raf.writeInt(age);
		raf.writeUTF(name);
		raf.writeDouble(height);
	}
	
	public void read(RandomAccessFile raf)throws IOException{
		this.age = raf.readInt();
		this.name = raf.readUTF();
		this.height = raf.readDouble();
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}
}