从FILEINPUT文本文件中读取,在输出窗口中显示信息,并创建一个新文本FILEOUTPUT,其中包含输出中的所有信息。
我认为我必须结合fileread和filewrite类?我对吗?如果那是我必须做的,我该怎么做?我只是自学了如何创造这些,所以我是新手。
这就是我所说的:
写文件:
import javax.swing.*;
import java.io.*;
class fileWrite{
public static void main(String str[]){
String fileName = JOptionPane.showInputDialog("Please, enter the name of the output file");
try{
DataOutput f = new DataOutputStream(new FileOutputStream(fileName + ".txt"));
f.writeBytes("Teacher: Hello");
f.writeBytes("Student: Hi");
f.writeBytes("Teacher: Why are you late?");
f.writeBytes("Student: I slept in");
}
catch(Exception e){
String msg = e.toString();
System.out.println(msg);
}
}
}
阅读文件:
import javax.swing.*;
import java.io.*;
class fileRead{
public static void main(String str[]){
String fileName = JOptionPane.showInputDialog("Enter the name of the file I need to read from");
try{
DataInput f = new DataInputStream(new FileInputStream (fileName + ".txt"));
String txt = f.readLine();
while(txt != null){
System.out.println(txt);
txt = f.readLine();
}
}
catch (Exception e){
String msg = e.toString();
System.out.println(msg);
}
}
}
我如何将这两种方式结合起来?我必须从FILEINPUT(fileRead方式)获取信息并将其放在FILEOUTPUT。(fileWrite)但我不知道如何将两者结合起来