读写文本文件

时间:2013-12-02 20:34:45

标签: java file-io

我想编写一个程序,我们要求用户提供2个文件,第一个文件用于读取,第二个文件用于写入 第一个我们想要读取文件然后将信息切换为大写并将其保存到第二个文件 我不能在第二部分写任何帮助吗?

public class FileConverter 
{

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


    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter the filename for the first file");
    String filename = keyboard.nextLine();

    File file = new File(filename);
    Scanner inputFile = new Scanner (file);

    while(inputFile.hasNext())
    {
        String fileinfo = inputFile.nextLine();
        String uppercaseinfo1 = fileinfo.toUpperCase();
    }

    System.out.print("Enter the filename "
                + "for the second file");
    filename = keyboard.nextLine();
    PrintWriter outputFile = new PrintWriter(file);

    while(inputFile.hasNext())
        {
            outputFile.println();
        }
}
}

5 个答案:

答案 0 :(得分:0)

您需要close() PrintWriter

...
while(inputFile.hasNext())
{
     outputFile.println();
}

ouputFile.close();

此外,您不需要两个循环。只需在一个循环中进行传输。您需要确保有两个不同的File对象。一个用于输入,一个用于输出。使用不同的文件名。

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter the filename for the first file");
String filename = keyboard.nextLine();
File file = new File(filename);                  // file 1
Scanner inputFile = new Scanner (file);          // infile

System.out.print("Enter the filename "
        + "for the second file");
filename = keyboard.nextLine();
File file1 = new File(filename);                 // file 2
PrintWriter outputFile = new PrintWriter(file1); // outfile

while(inputFile.hasNext())
{
    String fileinfo = inputFile.nextLine();
    String uppercaseinfo1 = fileinfo.toUpperCase();
    outputFile.println(uppercaeinfo1);
}

outputFile.close();

答案 1 :(得分:0)

你需要一个FileWriter,然后关闭它:

FileWriter outFile = new FileWriter(filePath);
PrintWriter out = new PrintWriter(outFile);

out.println("stuff");

out.close();

答案 2 :(得分:0)

不仅缺少close()方法。解决方案有一些错误。

// corrected statements are marked with "!"
public static void main(String[] args) throws IOException
{


    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter the filename for the first file");
    String filename = keyboard.nextLine();

    File file = new File(filename);
    Scanner inputFile = new Scanner(file);

    String uppercaseinfo1 = "";                             // !
    while(inputFile.hasNext())
    {
        String fileinfo = inputFile.nextLine();
        uppercaseinfo1 += fileinfo.toUpperCase() + "\n";       // !
    }
    inputFile.close();          // !

    System.out.print("Enter the filename "
            + "for the second file");
    filename = keyboard.nextLine();
    file = new File(filename);                  // !
    PrintWriter outputFile = new PrintWriter(file);
    outputFile.println(uppercaseinfo1);         // !
    outputFile.close();                         // !
}

并且,如上所述,如果希望更改生效,则必须在结尾处关闭输入/输出流。

答案 3 :(得分:0)

使用BufferedWriter实例写入文件而不是PrintWriter。

e.g。

BufferedWriter bw = new BufferedWriter(new FileWriter(filename)); // create the write buffer

// to-do: EITHER surround with try-catch in order catch the IO Exception OR add throws declaration

bw.write("some text"); // content for the new line
bw.newLine();  // a line break

bw.flush(); // flush the buffer and write into the file

答案 4 :(得分:0)

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


Scanner keyboard = new Scanner(System.in);

System.out.println("Enter the filename for the first file");
String filename = keyboard.nextLine();

File file = new File(filename);
Scanner inputFile = new Scanner (file);
System.out.print("Enter the filename "
            + "for the second file");
String filename2 = keyboard.nextLine();
File file2 = new File(filename2);
PrintWriter outputFile = new PrintWriter(file2);

while(inputFile.hasNext())
{
    String fileinfo = inputFile.nextLine();
    String uppercaseinfo1 = fileinfo.toUpperCase();
    outputFile.println(uppercaseinfo1);
}
 outputFile.close()
}
}

我这样做是