如何从文件中读取数据并将其放入变量并输出到其他文件。 Java的

时间:2013-07-15 20:02:40

标签: java

在这里介绍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();
        }       

    }
}

3 个答案:

答案 0 :(得分:3)

您还可以用于创建新文件

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文件

答案 1 :(得分:0)

尝试使用BufferedReader

BufferedReader pw = new BufferedReader(new FileReader(fileName)); 
String s = null;
s = pw.readLine();

工作示例

public static void main(String[] args) throws IOException {
        Scanner keyboard = new Scanner(System.in);
        String filePath = keyboard.next();

        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));

        String line = bufferedReader.readLine();
        System.out.println(line);
    }

在控制台上输入

的路径
C:\Users\path\Desktop\1.txt

您可以使用PrintWriter撰写

PrintWriter out   = new PrintWriter(new BufferedWriter(new FileWriter(fileName2)));

答案 2 :(得分:0)

您的代码无法编译。

while (filename.hasNextLine()) // String#hasNextLine() does not exist

hasNextLine()属于Scanner,它不用于读取文件,只是用于键盘上的控制台按键。

如果您打算在那里使用outputFile;这不会起作用,因为你不能使用PrintWriter作为文件阅读器。嗯,这个名字很明显。不是吗?

但是,除非您正在格式化输出,否则您应该避免使用PrintWriter进行写入。对于普通字符输出,首选FileWriter(包含在BufferedWriter内以获得性能)。同样,对于阅读文件,请选择FileReader(再次包含在BufferedReader内)。

以下是您的代码的外观:

public static void main(String[] Args) throws IOException
{
    // create the scanner for console 
    Scanner keyboard = new Scanner(System.in);

    // read the input/output file names
    System.out.print("Enter the name of a file: ");
    String inFile = keyboard.nextLine();

    System.out.print("Enter the name of a second file: ");
    String outFile = keyboard.nextLine();

    // close the scanner
    keyboard.close();

    // open file streams
    BufferedReader reader = new BufferedReader(new FileReader(inFile));
    BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));

    // copy the data (line by line)
    String text = null;
    while ((text = reader.readLine()) != null)
    {
        writer.write(text);
        writer.newLine();
    }

    // close the file streams
    reader.close();
    writer.close();
}