Java入门基础(锐格实验6)

在这里插入图片描述

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

public class Main {

    public static void main(String[] args) {
    	Scanner reader = new Scanner(System.in);
		int n =-1;
		String a;
		a = reader.next();
		byte [] b = a.getBytes();
		byte [] c = new byte[100];
		File file = new File("a.txt");
		try{
			OutputStream out = new FileOutputStream(file);
			out.write(b);
			out.close();
		}
		catch(IOException e) {
			System.out.println("Error "+e);
		}
		
		try{
			InputStream in = new FileInputStream(file);
			while((n=in.read(c, 0, 100))!=-1) {
				String s = new String (c,0,n);
				System.out.println("读取到的文件内容为:"+s);
			}
			in.close();
		}
		catch(IOException e) {
			System.out.println("File read Error"+ e);
		}
	}

}

在这里插入图片描述

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) { 
    String source=null,target=null;
    try { 
		FileInputStream fileread = new FileInputStream(new File("a.txt"));
		int length = fileread.available(); 
		byte[] buffer = new byte[length]; 
		fileread.read(buffer); 
		source = new String(buffer);
		fileread.close();
	} catch (Exception e) { 
		System.out.println("a");
		System.exit(0);
								}
		if(source==null)
			System.out.println("a.txt为空");
		else{
			System.out.println(source);
			target=zhuanhuan(source);
			System.out.println(target);
			try {
				FileOutputStream out = new FileOutputStream(new File("b.txt"));
				out.write(target.getBytes());//写入
				out.close();
			} catch (FileNotFoundException e1) {
				System.out.println("a");
				System.exit(0);
			} catch (IOException e) {
				System.out.println("a");
				System.exit(0);
			}
		}
	}
public static String zhuanhuan(String s){
	char []array = s.toCharArray();
	for(int i=0;i<array.length;i++){
		int j = (int)array[i];
		if(j>=65&&j<=90){
			if(j==88) j=65;
			else if(j==89) j=66;
			else if(j==90) j=67;
			else j=j+3; 
			array[i]=(char)j;
		}
		if(j>=97&&j<=122){
			if(j==120) j=97;
			else if(j==121) j=98;
			else if(j==122) j=99;
			else j=j+3; 
			array[i]=(char)j;
		}
	}
	return new String(array);
		}
}

在这里插入图片描述

import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
    int score=0;
    String result="ABCD";
	StringBuffer answer=new StringBuffer();
	Scanner reader=new Scanner(System.in);
	try {
		FileReader inOne=new FileReader("test.txt");
		BufferedReader inTwo=new BufferedReader(inOne);
		String s=null;
		while((s=inTwo.readLine())!=null) {
			if(!s.startsWith("*")) {
				System.out.println(s);
			}
			else {
				System.out.println("Please Input the Answer!");
			    String str=reader.nextLine();
			    try {
			    	char c=str.charAt(0);
			    	answer.append(c);
			    }catch(StringIndexOutOfBoundsException exp) {
			    	answer.append("*");
			    }
				}
		}
		inOne.close();
		inTwo.close();
	}catch(IOException exp) {
		System.out.println("a");
		System.exit(0);
	}
	for(int i=0;i<result.length();i++) {
		if(result.charAt(i)==answer.charAt(i)||result.charAt(i)==answer.charAt(i)-32) 
			score++;
	}
	System.out.println("The final goal is"+score);
	}
}

在这里插入图片描述

import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) { 
		Scanner reader=new Scanner(System.in);
		LinkedList<User>users=new LinkedList<User>();
		for(int i=0;i<5;i++) {
			User user=new User(reader.nextInt(), reader.next());
			users.add(user);
		}
		try {
			FileOutputStream a=new FileOutputStream("a.txt");
			ObjectOutputStream b=new ObjectOutputStream(a);
			b.writeObject(users);
			a.close();
			b.close();
		}catch(IOException e) {}
		
		
		try {
			FileInputStream c=new FileInputStream("a.txt");
			ObjectInputStream d=new ObjectInputStream(c);
			LinkedList<User>us=(LinkedList<User>)d.readObject();
			Iterator<User>ite=us.iterator();
			while(ite.hasNext()) {
				User it=ite.next();
				System.out.println("Account:"+it.getZh()+"   Password:"+it.getPw());
			}
		}catch(IOException e) {}
		catch(ClassNotFoundException e) {}
	}
}
class User implements Serializable{
	private static final long serialVersionUID = -1650301787760861093L;
	int zh;
	String pw;
	public User(int Zh, String Pw) {
		this.zh=Zh;
		this.pw=Pw;
	}
	public int getZh() {
		return zh;
	}
	public void setZh(int Zh) {
		this.zh=Zh;
	}
	public String getPw() {
		return pw;
	}
	public void setPw(String Pw) {
		this.pw=Pw;
	}
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值