文件I/O:从一个文件读取并写入另一个文件(Java)
我目前在我的cpe课程中的一个实验室工作,我们必须创建一个简单的程序,从.txt文件扫描字符串并将其打印到另一个.txt文件。到目前为止,我已经制定了基本程序,但我的异常不断得到抛出,虽然我有所有必要的文件。有人能帮我调试吗文件I/O:从一个文件读取并写入另一个文件(Java),java,file,io,Java,File,Io,我目前在我的cpe课程中的一个实验室工作,我们必须创建一个简单的程序,从.txt文件扫描字符串并将其打印到另一个.txt文件。到目前为止,我已经制定了基本程序,但我的异常不断得到抛出,虽然我有所有必要的文件。有人能帮我调试吗 import java.io.*; import java.util.*; public class FileIO { public static void main(String args[]) { try { File inp
import java.io.*;
import java.util.*;
public class FileIO {
public static void main(String args[]) {
try {
File input = new File("input");
File output = new File("output");
Scanner sc = new Scanner(input);
PrintWriter printer = new PrintWriter(output);
while(sc.hasNextLine()) {
String s = sc.nextLine();
printer.write(s);
}
}
catch(FileNotFoundException e) {
System.err.println("File not found. Please scan in new file.");
}
}
}
使用Java I/O访问文件时,必须包含文件的文件类型扩展名(如果存在)
您需要找出它在哪里查找
“input”
文件。当您只指定“input”
时,它会在当前工作目录中查找文件。使用IDE时,此目录可能不是您认为的目录
请尝试以下操作:
System.out.println(new File("input").getAbsolutePath());
查看它在哪里查找文件。可能是您忘记了
flush()
我们可以使用FileInputStream对象读取文件,然后使用FileOutputStream对象写入另一个文件 下面是示例代码
package java_io_examples;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Vector;
public class Filetest {
public static void main(String[] args) {
try {
FileInputStream fin = new FileInputStream("D:\\testout.txt");
int i = 0;
String s = "";
while((i=fin.read())!=-1) {
s = s + String.valueOf((char)i);
}
FileOutputStream fout = new
FileOutputStream("D:\\newtestout1.txt");
byte[] b = s.getBytes();
fout.write(b);
fout.close();
System.out.println("Done reading and writing!!");
} catch(Exception e){
System.out.println(e);
}
}
}
你的输入/输出文件没有扩展名?他已经说它们是.txt文件。。。看来这就是问题所在。给你计划在其上编程的任何计算机一点建议:将你的文件浏览器设置为始终显示文件扩展名。别忘了关闭扫描仪和书写器。他说他遇到了一个异常。@aioobe你的异常是什么?我没有任何异常,迈克(发布问题的人)有。引发异常是因为找不到文件,而不是因为打印机未能刷新其所有字节。我知道这是一个非常旧的答案/问题,但他确实需要调用
printer.close()
来完成对文件的写入。我以前有.txt扩展名,但我仍然会引发异常。我正在使用eclipse,.txt文件与.java文件一起放在src文件夹中。我重新添加了扩展名并将文件移到了正确的文件夹中,但现在每次运行我的程序时,我的output.txt都会更新,但当我打开它时,仍然没有任何内容。谢谢!!原来我的.txt文件放错了文件夹。所以它找到了它们,但现在它没有从input.txt写入output.txt。Output.txt仍然为空。这是因为您没有调用flush。我建议您在完成后调用printer.close()(这将为您刷新)。谢谢!!现在可以了!我关闭了扫描仪,但没有关闭打印机。
try {
File input = new File("input");
File output = new File("output");
Scanner sc = new Scanner(input);
PrintWriter printer = new PrintWriter(output);
while (sc.hasNextLine()) {
String s = sc.nextLine();
printer.write(s);
}
**printer.flush();**
}
catch (FileNotFoundException e) {
System.err.println("File not found. Please scan in new file.");
}
package java_io_examples;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Vector;
public class Filetest {
public static void main(String[] args) {
try {
FileInputStream fin = new FileInputStream("D:\\testout.txt");
int i = 0;
String s = "";
while((i=fin.read())!=-1) {
s = s + String.valueOf((char)i);
}
FileOutputStream fout = new
FileOutputStream("D:\\newtestout1.txt");
byte[] b = s.getBytes();
fout.write(b);
fout.close();
System.out.println("Done reading and writing!!");
} catch(Exception e){
System.out.println(e);
}
}
}
public void readwrite() throws IOException
{
// Reading data from file
File f1=new File("D:/read.txt");
FileReader fr=new FileReader(f1);
BufferedReader br=new BufferedReader(fr);
String s = br.readLine();
// Writing data
File f2=new File("D:/write.txt");
FileWriter fw=new FileWriter(f2);
BufferedWriter bw=new BufferedWriter(fw);
while(s!=null)
{
bw.write(s);
bw.newLine();
System.out.println(s);
s=br.readLine();
}
bw.flush();
bw.close();
}