Java入门基础(锐格实验6)
Posted 晨沉宸辰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}
}
以上是关于Java入门基础(锐格实验6)的主要内容,如果未能解决你的问题,请参考以下文章