I/O Filestreams in Java

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2004
Posts: 2
Reputation: kalpana_shukla is an unknown quantity at this point 
Solved Threads: 0
kalpana_shukla kalpana_shukla is offline Offline
Newbie Poster

I/O Filestreams in Java

 
0
  #1
Mar 25th, 2004
Hi,

I have a problem that my program is writing only single line from the dataset whereas I want to write whole data from the data file to be read and written on the screen. Somebody help me.

This is my program:

import java.io.*;
import java.util.*;
class QueryAnswering
{
int rowmax = 100;
int colmax = 17;
ArrayList data;
StringTokenizer strTokenizer;
String dataSetFileName = new String("data.txt");
File outputFile;
FileWriter out;
BufferedWriter outbuf;
public void readdata(String line1, int row) {
strTokenizer = new StringTokenizer(line1);
int[] rowData = new int[colmax];
for (int col = 0; col < colmax; col++) {
rowData[col] = Integer.parseInt(strTokenizer.nextToken());
}
data.add(rowData);
}

void readDataFile() {
File inputFile;
FileReader in;
BufferedReader inbuf;
int i = 0;
String line = null;
try {
inputFile = new File(this.dataSetFileName);
in = new FileReader(inputFile);
inbuf = new BufferedReader(in);
if ((rowmax == 0) || (colmax == 0)) {
line = inbuf.readLine();
strTokenizer = new StringTokenizer(line);
rowmax = Integer.parseInt(strTokenizer.nextToken());
colmax = Integer.parseInt(strTokenizer.nextToken());
}
data = new ArrayList();
while (i < rowmax) {
line = inbuf.readLine();
readdata(line, i);
i++;
}
inbuf.close();
in.close();
} catch (Exception ex) {
System.out.println(line + i);
//ex.printStackTrace();
}

}

public static void main(String[] args) throws IOException {
QueryAnswering d = new QueryAnswering();
d.readDataFile();

}
}

Data.txt


republican n y n y y y n n n y ? y y y n y
republican n y n y y y n n n n n y y y n ?
democrat ? y y ? y y n n n n y n y y n n
democrat n y y n ? y n n n n y n y n n y
democrat y y y n y y n n n n y ? y y y y
democrat n y y n y y n n n n n n y y y y
democrat n y n y y y n n n n n n ? y y y
Reply With Quote Quick reply to this message  
Join Date: Feb 2004
Posts: 16
Reputation: CodeMasterFlex is an unknown quantity at this point 
Solved Threads: 1
CodeMasterFlex's Avatar
CodeMasterFlex CodeMasterFlex is offline Offline
Newbie Poster

Re: I/O Filestreams in Java

 
0
  #2
Mar 26th, 2004
What exactly is this program supposed to do? Please post how the data should look in it's correct form.
-=CodeMasterFlex=-

Mastering Code One old school party at a time.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 2
Reputation: kalpana_shukla is an unknown quantity at this point 
Solved Threads: 0
kalpana_shukla kalpana_shukla is offline Offline
Newbie Poster

Re: I/O Filestreams in Java

 
0
  #3
Mar 27th, 2004
I solved the above problem.
I want to read random lines with few selected columns from text file, can you give idea of how to do it.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 7
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

Re: I/O Filestreams in Java

 
0
  #4
Mar 27th, 2004
//Pre conditions: MAX_LINES is an interger that "knows" the number of lines the file has
//heres a little something, its more like psedo code

...Somewhere in the method...
int randomLine = nextInt(MAX_LINES); // Gets a random interger from 0 to end of file

while (int i = 0; i < randomLine; i++)
input.readLine(); // goes to the the random line - 1

String line = input.readLine()); // now that we're at random line -1 read it again and it will be the random line, now actually store the line.
...Do something with the line read in...

hope thats helpful, not sure on the columns
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC