2013-07-15 52 views
0

这里介绍java class tard。我试图从一个文件读取数据,然后操纵到一个不同的文件并保存。我认为我很接近,但有问题一起使用扫描仪和.IO。任何帮助都会很棒。如何从文件读取数据并将其放入变量并输出到不同的文件。 Java

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(); 
     }  

    } 
} 

回答

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))); 
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(); 
} 
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); 
    } 
} 

这是一个很好的做法,因为你没有关闭流。

这会产生与预期输出使用现有的解决方案,以一个共同的问题,而不是你自己发明的

+0

+1很好的例子test.txt文件。 – Brandon

相关问题