954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

I/O Filestreams in Java

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

kalpana_shukla
Newbie Poster
2 posts since Mar 2004
Reputation Points: 10
Solved Threads: 0
 

What exactly is this program supposed to do? Please post how the data should look in it's correct form.

CodeMasterFlex
Newbie Poster
16 posts since Feb 2004
Reputation Points: 10
Solved Threads: 1
 

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.

kalpana_shukla
Newbie Poster
2 posts since Mar 2004
Reputation Points: 10
Solved Threads: 0
 

//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 :sad:

BountyX
Posting Whiz in Training
230 posts since Mar 2004
Reputation Points: 28
Solved Threads: 9
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You