Home arrow static arrow Java Programming [Archive] - i/o:streams to another directory
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - i/o:streams to another directory
This topic has 5 replies on 1 page.

Posts:9
Registered: 7/15/04
i/o:streams to another directory  
Jul 15, 2004 2:12 PM



 
Hello all,
I have a very minimal knowledge base on File I/O and data streams, enough to get the job done. What i dont know however is how to open an input or output stream that will read from or write to a file in a directory other than the one where the source code is located. I believe of the top of my head you would create an input stream like so: BufferedReader name = new BufferedReader(new FileReader(filename)); . However if i have to create and recall from a large array of file it is very inconvinent to store them in the same directory. Please, someone let me know if there is something i can do. Thanks in advance.
Bryan
 

Posts:3,258
Registered: 00-08-28
Re: i/o:streams to another directory  
Jul 15, 2004 2:37 PM (reply 1 of 5)



 
class fileread{  public static void main(String args[]) throws IOException{   int i;  FileInputStream fin=new FileInputStream("c:\\input.txt");  do{     i=fin.read();     System.out.println((char)i);     }while(i != -1);        fin.close();     fin=null;    }    }  
 

Posts:3,258
Registered: 00-08-28
Re: i/o:streams to another directory  
Jul 15, 2004 2:38 PM (reply 2 of 5)



 
you could also use something like
try {  FileInputStream fis = new FileInputStream(f);  if (f.length() > Integer.MAX_VALUE)    System.out.println("File '" + f.getName() + "' is too big.");  int buffersize = (int)f.length(); byte[] contents = new byte[buffersize];;  long n = fis.read(contents, 0, buffersize); fis.close(); fis = null;  if (n != f.length())    System.out.println("Error in reading file '" + f.getName() + "'"); Else    System.out.println( new String(contents) );  } catch (Exception e) {} 
 

Posts:3,258
Registered: 00-08-28
Re: i/o:streams to another directory  
Jul 15, 2004 2:39 PM (reply 3 of 5)



 
Ig however you want to use FileReader here it is

try {        String line = null;    BufferedReader br = new BufferedReader(new FileReader("oldFileName"));    PrintWriter pr = new PrintWriter(new BufferedWriter(new FileWriter("newFileName", false)));                     while ((line = br.readLine()) != null) {         pr.println(line);    }      br.close();                    br = null;             pr.close();    pr = null;                      } catch (java.io.FileNotFoundException ex) {    System.out.println("File '" + args[0] + "' does not exist. ");         } catch (java.io.IOException ex) {    ex.printStackTrace(); }  
 

Posts:3,055
Registered: 18/06/98
Re: i/o:streams to another directory  
Jul 15, 2004 2:39 PM (reply 4 of 5)



 
Do you know how to work with directory names?

Just specify the name of the file with the directory name. For instance, if you are working in the directory "C:\WORK" and using the file "myworkfile" and the huge data file is in the directory "C:\DATA" and has the name "hugedatafile" you can access your files as "c:/work/myworkfile" and "c:/data/hugedatafile". (note that using "/" instead of "\" does not hurt and is easier to write in your Java program.)
 

Posts:9
Registered: 7/15/04
Re: i/o:streams to another directory  
Jul 15, 2004 2:56 PM (reply 5 of 5)



 
Thank you all for your help. I did not realize the the answer was as simple as specifying the directory. I am amazed that i could get such a timely and informative response. :P
 
This topic has 5 replies on 1 page.