英文:
Why can't I write in my file using a FileWriter on my local host but it works on a other machine?
问题
我想在文件中写入行,在我的本地机器上,文件位于`/home/localuser/Desktop/myProgram/files`,当我将它们放在服务器机器上时,它们位于`/userhome/my_name`。
当我在本地机器上运行程序时,它会在`/home/localuser/Desktop/myProgram/files/output.txt`下创建输出文件,并且会检测此文件是否已经存在,但它不会向其中写入任何内容,也不会引发任何异常。在服务器上,它会在`/userhome/my_name/output.txt`下创建并检测文件,并且会向其中写入内容。
以下是创建文件并在文件已存在时将其清空的代码部分:
```java
private static void createFile(String fileName){
try{
File printerFile = new File(fileName);
if(printerFile.createNewFile()){
System.out.println("文件已创建:" + printerFile.getName());
}else{
System.out.println("文件已存在。清空:" + fileName);
emptyFile(fileName);
}
} catch (IOException e){
System.out.println("创建新文件时出现问题");
e.printStackTrace();
}
}
private static void emptyFile(String filename){
try{
new PrintWriter(filename).close(); // 清空文件内容
}catch (IOException e){
System.out.println("清空文件时出现问题");
e.printStackTrace();
}
}
然后是写入内容的代码部分:
process Printer_P{
String messageToPrint; // 用于存储打印机从客户端接收到的消息
PrinterCommand command; // 用于存储客户端请求的操作
try{ // 尝试打开文件写入器
out = new FileWriter(filename,true);
}catch(IOException e){
System.out.println("在Printer.jr中打开新的FileWriter时出现问题");
e.printStackTrace();
}
System.out.println(printerName + " " + "开机");
while(true){
receive action(command, messageToPrint); // 等待接收操作
if(command == PrinterCommand.PRINT){
System.out.println(printerName + " " + messageToPrint);
try{ // 尝试写入操作
out.write(messageToPrint+"\n");
}catch(IOException e){
System.out.println("在Printer.jr中写入输出文件时出现问题");
e.printStackTrace();
}
}else if (command == PrinterCommand.CLOSE){
try{
out.close();
}catch (Exception e){
System.out.println("关闭Printer.jr中的文件写入器时出现问题");
e.printStackTrace();
}
break;
}
}
System.out.println(printerName + " " + "关机");
}
}
为什么在本地机器上使用时不会向文件中写入内容,而在服务器机器上使用时会写入呢?
<details>
<summary>英文:</summary>
I want to write lines in a file, on my local machine the file are under `/home/localuser/Desktop/myProgram/files` and when I put them on the server machine they are under `/userhome/my_name`.
When I run the program on my local machine, it creates the output file under `/home/localuser/Desktop/myProgram/files/output.txt` and it detects if this file already exists but it doesn't write in it, neither throws any excpetions. On the server it creates and detects the file under `/userhome/my_name/output.txt` and it WRITES in it.
The code is the following one to create and make the file empty if it already exists:
```java
private static void createFile(String fileName){
try{
File printerFile = new File(fileName);
if(printerFile.createNewFile()){
System.out.println("File created: " + printerFile.getName());
}else{
System.out.println("File already exists. Emptying " + fileName);
emptyFile(fileName);
}
} catch (IOException e){
System.out.println("Problem while creating new file");
e.printStackTrace();
}
}
private static void emptyFile(String filename){
try{
new PrintWriter(filename).close(); // make the file empty
}catch (IOException e){
System.out.println("Problem while emptying the file");
e.printStackTrace();
}
}
and then to write the content :
process Printer_P{
String messageToPrint; // used to store the message that the printer get from the client
PrinterCommand command; // used to store the action that the client asked for
try{ // try and catch the openning of a file writer
out = new FileWriter(filename,true);
}catch(IOException e){
System.out.println("Problem while openning a new FileWriter in Printer.jr");
e.printStackTrace();
}
System.out.println(printerName +" " + "POWER ON");
while(true){
receive action(command, messageToPrint); // wait until it receive an action
if(command == PrinterCommand.PRINT){
System.out.println(printerName + " " + messageToPrint);
try{ // try and catch the writting action
out.write(messageToPrint+"\n");
}catch(IOException e){
System.out.println("Problem while writting in the output file in Printer.jr");
e.printStackTrace();
}
}else if (command == PrinterCommand.CLOSE){
try{
out.close();
}catch (Exception e){
System.out.println("Problem while close the filewriter in Printer.jr");
e.printStackTrace();
}
break;
}
}
System.out.println(printerName +" " + "SHUTDOWN");
}
}
Why doesn't it write in the file when I'm using it on local machine but it do when it's on the server machine ?
专注分享java语言的经验与见解,让所有开发者获益!
评论