JAVA-IO相关知识点

1、掌握打印流以及Scanner类的常用方法,使用打印流和Scanner优化之前的FileInputStream与FileOutputStream操作。要求:在桌面上新建一个Test.txt,使用打印流向文件中输出如下: 
Hello 123 

hello bit 然后使用Scanner类读取文件内容并输入到控制台。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Homework2 {
	public static void main(String[] args) throws IOException {
		File file=new File("C:"+File.separator+"Users"+File.separator+
				"Administrator"+File.separator+"Desktop"+File.separator+
				"Test.txt");
		if(!file.exists()) {
			file.createNewFile();
		}
		OutputStream output=new FileOutputStream(file);
		String data="Hello 123\r\nhello bit\r\n";
		output.write(data.getBytes());
		output.close();
		
		
		File outputFile=new File("C:"+File.separator+"Users"+File.separator+
				"Administrator"+File.separator+"Desktop"+File.separator+
				"Test.txt");
		if(!outputFile.exists()) {
			System.out.println("文件不存在");
			return;
		}
		InputStream input=new FileInputStream(outputFile);
		byte[] inputData=new byte[1024];
		int len = input.read(inputData);
		String result =new String(inputData, 0, len);
		System.out.println("读取结果为"+result);
		input.close();
	}
}
2、要求自定义Person类,其中三个属性name,age,school. 
age属性不作为序列化保存而其他两个属性使用序列化保存在本地文件TestSer.txt中。 
使用序列化和反序列化的方式将自定义类序列化与反序列化操作。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.omg.CORBA.PUBLIC_MEMBER;

public class Homework3 {
	public static final File file=new File("C:"+File.separator+"Users"+File.separator+
			"Administrator"+File.separator+"Desktop"+File.separator+
			"TestSer.txt");	
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		Fun(new Person("X1m", 20, "XATU"));
		unFun();
		
	}
	public static void Fun(Object obj) throws FileNotFoundException, IOException {
		ObjectOutputStream objectOutputStream=new ObjectOutputStream(new FileOutputStream(file));
		objectOutputStream.writeObject(obj);
		objectOutputStream.close();
	}
	public static void unFun() throws FileNotFoundException, IOException, ClassNotFoundException {
		ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream(file));
		System.out.println(objectInputStream.readObject());
		objectInputStream.close();
	}
}
@SuppressWarnings("serial")
class Person implements Serializable{
	private String name;
	private transient Integer age;
	private String school;
	
	public Person(String name,Integer age,String school) {
		// TODO Auto-generated constructor stub
		super();
		this.name=name;
		this.age=age;
		this.school=school;
	}
	
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", school=" + school + "]";
	}
}
3、输入一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
package javaHomework4;

import java.util.Scanner;

public class Homework4 {
	public static String data;
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个五位数");
		if (scanner.hasNextInt()) {
			data=scanner.next();
		}
		Judge judge=new Judge();
		judge.setData(data);
		scanner.close();
		judge.judgeFun();
	}
}
class Judge{
	private String array;
	private char[] temp;
	public void setData(String data) {
		this.array=data;
		temp=array.toCharArray();
	}
	public void judgeFun(){
		
		if(temp[0] == temp[4]) {
			if(temp[1] == temp[3]) {
				System.out.println("是回文");
				return;
			}
			System.out.println("不是回文");
			return;
		}
		System.out.println("不是回文");
		return;
	}
}

你可能感兴趣的:(JAVA-IO相关知识点)