LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 12-02-2005, 10:58 PM   #1
irfanhab
Member
 
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

Rep: Reputation: 34
Can not write to file in Java


This program is supposed to read a file of the format:
asdf,asdf,sadf

And write a column into a file called var.txt, but it creates the file, and does not write anything to it. No exception occurs

Code:
import java.io.*;
import java.util.*;

public class colsplit {

 public static void main(String[] args)
 {
  
  Vector<String[]> vs = new Vector<String[]>();
  try
  {
   BufferedReader br = new BufferedReader(new FileReader(args[0]));
   
   while(br.ready())
   {
    StringTokenizer st = new StringTokenizer(br.readLine(),",");
    String[] arr = new String[st.countTokens()];
    for(int i=0;i<=st.countTokens();i++)
    {
     arr[i] = st.nextToken();
    }
    vs.add(arr);
    
   }
   
   
   
   BufferedWriter bw = new BufferedWriter(new FileWriter(new File("/root/var.txt")));
   
   for(int j=0;j<vs.size();j++)
   {
    
     bw.write(vs.elementAt(j)[Integer.parseInt(args[2])]  + "\n");
   }
    
  }catch(Exception e)
  {}
    
}
}
 
Old 12-02-2005, 11:20 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Have you considered that maybe "vs" has zero elements?
 
Old 12-03-2005, 01:59 AM   #3
irfanhab
Member
 
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

Original Poster
Rep: Reputation: 34
[QUOTEHave you considered that maybe "vs" has zero elements?[/QUOTE]

this is not possible as I intentionally valid data to it,

If I go like:
Code:
for(int i=0;i<vs.size;i++)
{System.out.println(vs.elementAt(i));
}
It does show valid output but never writes it to the file.
 
Old 12-03-2005, 02:59 AM   #4
ppanyam
Member
 
Registered: Oct 2004
Location: India
Distribution: Redhat
Posts: 88

Rep: Reputation: 15
Can you try bw.flush() after bw.write()?
 
Old 12-03-2005, 03:36 AM   #5
irfanhab
Member
 
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

Original Poster
Rep: Reputation: 34
yes ppanyam that worked. thanks!
but when you write shudnt the JVM itself handle the flushing, instead of calling it explicity.
 
Old 12-03-2005, 01:19 PM   #6
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Your program is supposed to do an explicit "close()". Which, in turn, does an implicit "flush()".
 
Old 12-04-2005, 11:49 PM   #7
ppanyam
Member
 
Registered: Oct 2004
Location: India
Distribution: Redhat
Posts: 88

Rep: Reputation: 15
The BufferedWriter "write()"s to buffer, as the name suggests. It will write to disk as per a predefined policy like if buffer is full or if x amount of time has lapsed. If you want it to write immediately to disk, you invoke flush() on the BufferedWriter. Alternately, if you close() the stream, it will clean up and will write to disk.
 
Old 12-05-2005, 12:01 AM   #8
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
ppanyam is right.

A "BufferedWriter" (by definition!) buffers its data (in the name of I/O efficiency). If you don't like this behavior, you can either flush whenever you wish to, or choose an unbuffered I/O class.

But in any case - you must absolutely "close()" any file you open. Doing otherwise (e.g. depending on the OS or JVM to "clean up after you") is, frankly, just sloppy programming.

IMHO .. PSM

Last edited by paulsm4; 12-05-2005 at 12:40 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grip : no write access to write encoded file bidouilleur Linux - Software 5 10-09-2010 09:23 PM
Write to file omerlh Programming 6 10-17-2005 04:02 AM
forrtl: severe (47): write to READONLY file, unit 5, file /dev/pts/1 terrence Programming 1 10-01-2005 10:22 PM
java io write file dummyagain Programming 3 02-23-2005 04:50 PM
Grip- "no write access to write encoded file" Alvis Linux - Software 4 01-06-2004 04:18 PM


All times are GMT -5. The time now is 02:37 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration