943,866 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5587
  • Java RSS
Mar 25th, 2004
0

I/O Filestreams in Java

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kalpana_shukla is offline Offline
2 posts
since Mar 2004
Mar 26th, 2004
0

Re: I/O Filestreams in Java

What exactly is this program supposed to do? Please post how the data should look in it's correct form.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
CodeMasterFlex is offline Offline
16 posts
since Feb 2004
Mar 27th, 2004
0

Re: I/O Filestreams in Java

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kalpana_shukla is offline Offline
2 posts
since Mar 2004
Mar 27th, 2004
0

Re: I/O Filestreams in Java

//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
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Round Robin algorithm?
Next Thread in Java Forum Timeline: Help with simple Java IO program running under cygwin





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC