Why can't I write in my file using a FileWriter on my local host but it works on a other machine?

huangapple 未分类评论45阅读模式
英文:

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&#39;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(&quot;File created: &quot; + printerFile.getName());
            }else{
                System.out.println(&quot;File already exists. Emptying &quot; + fileName);
                emptyFile(fileName);
            }
        } catch (IOException e){
            System.out.println(&quot;Problem while creating new file&quot;);
            e.printStackTrace();
        }
    }

private static void emptyFile(String filename){
        try{
            new PrintWriter(filename).close(); // make the file empty
        }catch (IOException e){
            System.out.println(&quot;Problem while emptying the file&quot;);
            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(&quot;Problem while openning a new FileWriter in Printer.jr&quot;);
            e.printStackTrace();
        }
 
        System.out.println(printerName +&quot; &quot; + &quot;POWER ON&quot;);
 
        while(true){
            receive action(command, messageToPrint); // wait until it receive an action
            if(command == PrinterCommand.PRINT){
                System.out.println(printerName + &quot; &quot; + messageToPrint);
                try{ // try and catch the writting action
                    out.write(messageToPrint+&quot;\n&quot;);
                }catch(IOException e){
                    System.out.println(&quot;Problem while writting in the output file in Printer.jr&quot;);
                    e.printStackTrace();
                }
            }else if (command == PrinterCommand.CLOSE){
                try{
                    out.close();
                }catch (Exception e){
                    System.out.println(&quot;Problem while close the filewriter in Printer.jr&quot;);
                    e.printStackTrace();
                }
                break;
            }
        }
        System.out.println(printerName +&quot; &quot; + &quot;SHUTDOWN&quot;);
    }
}

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 ?

huangapple
  • 本文由 发表于 2020年4月8日 00:53:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/61085241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定