这里介绍java类tard。我正在尝试从文件中读取数据,然后操作到不同的文件并保存它。我想我已经很接近了,但同时使用扫描仪和 .IO 时遇到问题。任何帮助都会很棒。
import java.util.Scanner;
import java.io.*;
public class fileswitch
{
public static void main(String[] Args) throws IOException
{
String filename;
String filename2;
String text;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the name of a file: ");
filename = keyboard.nextLine();
PrintWriter outputFile = new PrintWriter(filename);
System.out.print("Enter the name of a second file: ");
filename2 = keyboard.nextLine();
PrintWriter outputFile2 = new PrintWriter(filename2);
while (filename.hasNextLine())
{
text = filename.readAllLines();
text = text.toUpperCase();
outputFile2.print(text);
outputFile2.close();
}
}
}
最佳答案
您还可以用于创建新文件
package test;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class WriteStringToFile {
public static void main(String[] args) throws IOException {
String string = "This is\na test";
File file = new File("test.txt");
FileUtils.writeStringToFile(file, string);
}
}
这是一个很好的做法,因为您不必关闭流。
这会生成具有预期输出的 test.txt 文件
关于java - 如何从文件读取数据并将其放入变量并输出到不同的文件。 java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17662846/