Java Printwriter对象未写入文件的println()
为什么PrintWriter没有在下面的代码中写入文件Java Printwriter对象未写入文件的println(),java,printstream,Java,Printstream,为什么PrintWriter没有在下面的代码中写入文件 import java.io.*; import java.util.*; class test{ public static void main(String[] args) throws IOException{ Scanner in = new Scanner(System.in); System.out.println("Enter input file name"); S
import java.io.*;
import java.util.*;
class test{
public static void main(String[] args) throws IOException{
Scanner in = new Scanner(System.in);
System.out.println("Enter input file name");
String inputfile = in.nextLine();
System.out.println("Enter output file name");
String outputfile = in.nextLine();
in.close();
File f = new File(inputfile);
Scanner input = new Scanner(f);
PrintWriter output = new PrintWriter(outputfile);
while( input.hasNextLine()){
String s = input.nextLine(); ////// reading the file lines perfectlly
output.print(s); // but not writing
}
output.close();
input.close();
}
}
如代码中所述,正在读取输入文件的行,但未写入输出文件。此代码似乎正常,您在哪里检查了输出文件(文件路径)?您的输出文件是否具有写入权限?您将向
outputfile
馈送什么?有写权限都可以吗?@lynxx-PrintWriter
可以写没有扩展名的文件。只是它不会自动添加扩展。为什么?因为它不知道正确的扩展应该是什么!(文件扩展名是一种特定于操作系统的(即不可移植的)约定。Java i/O并不特别关注它们。)我同意@StephenC。刚刚在Ubuntu 18.04上测试过。您仍然会得到一个没有扩展名的文件,它可以作为普通文本文件打开。可能是您不希望它出现在那里。此代码似乎没有问题,您在哪里检查了输出文件(文件路径)?您的输出文件是否具有写入权限?您将向outputfile
馈送什么?有写权限都可以吗?@lynxx-PrintWriter
可以写没有扩展名的文件。只是它不会自动添加扩展。为什么?因为它不知道正确的扩展应该是什么!(文件扩展名是一种特定于操作系统的(即不可移植的)约定。Java i/O并不特别关注它们。)我同意@StephenC。刚刚在Ubuntu 18.04上测试过。您仍然会得到一个没有扩展名的文件,它可以作为普通文本文件打开。可能是你没想到它会在那里。