简体   繁体   English

无法写入Java中的远程文件系统

[英]Cannot write to a remote file system in java

I'm using Play! 我正在使用Play! framework. 框架。 I want to access remote file (read & write) connected through LAN. 我想访问通过LAN连接的远程文件(读和写)。 While accessing to read a file for a first time in java. 在Java中首次访问以读取文件时。 I was unable to read the file. 我无法读取该文件。 If i load the URL in browser once (successfully) then i am able to read the file in java too. 如果我一次(成功)将URL加载到浏览器中,那么我也可以用Java读取文件。 ( I am reading using HttpURLConnection) Now i want to write into the file which i can't. (我正在使用HttpURLConnection进行读取)现在我想写入无法写入的文件。 There are no errors or exceptions. 没有错误或例外。 But the content is not written to the file. 但是内容未写入文件。 I have given 777 permission to my Play! 我已授予777游戏许可! application. 应用。 What could be the issue. 可能是什么问题。 How can i solve it. 我该如何解决。

EDIT: 编辑:

To update the file i have written this code 要更新文件,我已编写此代码

public void createFileInPath(String filePath, Object contents)
   {
        try{

        Writer output = null;
        String text = contents.toString();
        File file = new File(filePath);
        output = new BufferedWriter(new FileWriter(file));
        output.write(text);
        output.close();

        }catch (Exception e){
        System.err.println("Error While Creating File in FileManager.java: " + e);
        }
   }

Try using java.io.* classes. 尝试使用java.io. *类。 You dont have to treat it specially if it is already mounted on your system (linux) or mapped(windows). 如果已将其安装在系统(linux)或已映射(windows)上,则无需特别对待。

All you need to do is pass the right path and operate as if it is a local file. 您需要做的就是传递正确的路径并像本地文件一样进行操作。

To read a file: 读取文件:

    BufferedReader in = new BufferedReader(new FileReader("my file path"));
    String line = null;
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
    in.close();
    // To write into a file
    PrintWriter out = new PrintWriter(new FileWriter("my file path"));
    out.println("some content");
    out.flush(); // required to flush the content to filesystem
    out.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM