| | |
I/O Filestreams in Java
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2004
Posts: 2
Reputation:
Solved Threads: 0
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
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
//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
//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
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
![]() |
Similar Threads
- FT Junior Java Developer Needed for Major Media firm in NYC (Software Development Job Offers)
- Java/J2EE Senior Software Engineer (Software Development Job Offers)
- Java Developer Required (Software Development Job Offers)
Other Threads in the Java Forum
- Previous Thread: Round Robin algorithm?
- Next Thread: Help with simple Java IO program running under cygwin
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited variablebinding webservices windows





