谁能帮我做个东西,编写一个JAVA的程序

2025-02-23 15:12:08
推荐回答(4个)
回答1:

已解决~!

回答2:

这个太简单了.....我肯定能给你做出来.......加QQ聊吧 ,光给你,你可能没法用,我给你讲讲就行了

回答3:

我给你写这里,我电脑不支持中文环境,所以只能用英文测试了,OK的
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

public class FileWriter {

public static void main(String[] args) throws IOException {

File file = new File("C:\\aaa.txt");

//Create file
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}

String context = "(班级0820.学号08419020 姓名huangyan年龄21籍贯江苏)";

writeToFile(file, context); //write to file

String contextInFile = readFromFile(file);//read again from file

System.out.println(contextInFile);

}

private static String readFromFile(File file) throws IOException {
FileReader fl = new FileReader(file);
BufferedReader bf = new BufferedReader(fl);
String context = bf.readLine();

bf.close();
return context;

}

private static void writeToFile(File file, String context) throws FileNotFoundException, IOException {
FileOutputStream fl = new FileOutputStream(file);
fl.write(context.getBytes());

fl.close();
}
}

------
class0820. Sno08419020 Name HuangYan Age: 21 Add Jiangsu

回答4:

import java.io.BufferedReader;
import java.io.BufferedWriter;
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.util.Scanner;

public class FileSolve {

public static void main(String[] args) {
String string=null;
Scanner ss=new Scanner(System.in);
System.out.println("Please input your information");
string =ss.nextLine();
try {
FileOutputStream fos=new FileOutputStream("c:/aaa.txt");
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fos));
System.out.println("Write file"+string);
bw.write(string);
bw.flush();
fos.close();
bw.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("c:/aaa.txt")));
System.out.println(br.readLine());
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ss.close();
System.out.println("System exit");

}

}