This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing File from other computer in network

 
Jonk kulsh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting problem in accessing file from other computer in the network. computer drive is shareble.



import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;

public class ReadWriteTextFile {

private static void doReadWriteTextFile() {

try {

// input/output file names
String inputFileName = "\\192.165.0.5\\C:\\demo.java";
String outputFileName = "C:\\Demo\\demo1.java";

// Create FileReader Object
FileReader inputFileReader = new FileReader(inputFileName);
FileWriter outputFileReader = new FileWriter(outputFileName);

// Create Buffered/PrintWriter Objects
BufferedReader inputStream = new BufferedReader(inputFileReader);
PrintWriter outputStream = new PrintWriter(outputFileReader);

// Keep in mind that all of the above statements can be combined
// into the following:
//BufferedReader inputStream = new BufferedReader(new FileReader("README_InputFile.txt"));
//PrintWriter outputStream = new PrintWriter(new FileWriter("ReadWriteTextFile.out"));

outputStream.println("+---------- Testing output to a file ----------+");
outputStream.println();

String inLine = null;

while ((inLine = inputStream.readLine()) != null) {
outputStream.println(inLine);
}

outputStream.println();
outputStream.println("+---------- Testing output to a file ----------+");

outputStream.close();
inputStream.close();

} catch (IOException e) {

System.out.println("IOException:");
e.printStackTrace();

}

}


public static void main(String[] args) {
doReadWriteTextFile();
}

}


----------------------------------------------
Error i'm getting
----------------------------------------------

IOException:
java.io.FileNotFoundException: \192.165.0.5\C:\demo.java (The filename, director
y name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41)
at ReadWriteTextFile.doReadWriteTextFile(ReadWriteTextFile.java:19)
at ReadWriteTextFile.main(ReadWriteTextFile.java:57)
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In ...


... you needed four back-slashes at the start. That's because you really want two back-slashes, but you need an extra back-slash to "escape" each one.

Of course, such a path only works in the world of Windows. Have you considered using "file:" URLs instead, which might be more portable?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic