2011-12-29 94 views
0

我正在使用Play!框架。我想访问通过LAN连接的远程文件(读取&写入)。在java中第一次访问时读取文件。我无法阅读该文件。如果我一次(成功)在浏览器中加载URL,那么我也能够在Java中读取文件。 (我正在阅读使用HttpURLConnection)现在我想写入文件,我不能。没有错误或例外。但内容不写入文件。我给了我的Play 777许可!应用。可能是什么问题。我该如何解决它。无法在java中写入远程文件系统

编辑:

要更新我写了这个代码

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); 
     } 
    } 
+0

检查正常的冲洗/流接近将是我的第一个猜测。张贴代码以获得进一步的帮助。 – Nrj 2011-12-29 04:30:10

+0

你是否提交了你在流中写入的数据?关闭流自动做 – Saurabh 2011-12-29 04:32:11

+0

“远程文件”,通过什么样的文件系统访问? NFS? SMB? CIFS? SSHFS? AFS? AFP? NCP?结尾?间奏曲? S3?光泽? – sarnold 2011-12-29 04:40:33

回答

0

尝试使用java.io. *类的文件。如果它已经安装在您的系统(linux)或映射(windows)上,您不必特别对待它。

您只需传递正确的路径并像操作本地文件一样操作即可。

要读取文件:

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(); 
+0

如果要读取和写入,请使用此http://docs.oracle.com/javase/1.4.2/docs/api/java/io/RandomAccessFile.html – 2011-12-29 05:27:34

+0

如果要处理对象而不是字符串,则请使用http://docs.oracle.com/javase/1.4.2/docs/api/java/io/ObjectInputStream.html和http://docs.oracle.com/javase/1.4.2/docs/api/java /io/ObjectOutputStream.html – 2011-12-29 05:29:05

+0

我给出了http:/ localhost:9000/public/CacheLayer/Customer_Bmtech/UsersList/UsersList.list,它在浏览器中打开,但抛出FileNotFoundException:http:/ localhost:9000/public/CacheLayer/Customer_Bmtech /UsersList/UsersList.list(没有这样的文件或目录) – Arasu 2011-12-29 06:14:57