aspose file tools*
The moose likes Beginning Java and the fly likes text file into an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "text file into an array" Watch "text file into an array" New topic
Author

text file into an array

Ed Byrnes
Ranch Hand

Joined: Feb 04, 2001
Posts: 33
I found the following program which reads each line of a text file (mydata.txt) as a record and then prints it to the screen.
What I want to do is put each of the "records" into an array.
Can anyone show me how to do this? Thanks...
// FileReadTest.java
// Copyright 1998 DevDaily Interactive, Inc. All Rights Reserved.
import java.io.*;
import java.util.*;
class FileReadTest {
//--------------------------------------------------< main >--------//
public static void main (String[] args) {
FileReadTest t = new FileReadTest();
t.readMyFile();
}

//--------------------------------------------< readMyFile >--------//
void readMyFile() {
String record = null;
int recCount = 0;
try {
FileReader fr = new FileReader("mydata.txt");
BufferedReader br = new BufferedReader(fr);
record = new String();
while ((record = br.readLine()) != null) {
recCount++;
System.out.println(recCount + ": " + record);
}
} catch (IOException e) {
// catch possible io errors from readLine()
System.out.println("Uh oh, got an IOException error!");
e.printStackTrace();
}
} // end of readMyFile()
} // end of class
Ed Byrnes
Ranch Hand

Joined: Feb 04, 2001
Posts: 33
Okay, I have it working. It now prints the list of records and then gives a randomized list. All I really want to do is write a program which will create a text file of names, retrieve the file, randomize the names, then print the original order in a column and the ranomized order in a column next to it.
// FileReadTest.java
// Copyright 1998 DevDaily Interactive, Inc. All Rights Reserved.
import java.io.*;
import java.util.*;
class FileReadTest {
//--------------------------------------------------< main >--------//
public static void main (String[] args) {
FileReadTest t = new FileReadTest();
t.readMyFile();
}

//--------------------------------------------< readMyFile >--------//
void readMyFile() {
String record = null;
int recCount = 0;
String[] original = new String[8];

try {
FileReader fr = new FileReader("mydata.txt");
BufferedReader br = new BufferedReader(fr);
record = new String();
while ((record = br.readLine()) != null) {
original[recCount] = new String(record);
recCount++ ;
System.out.println(recCount + ": " + record);
}
} catch (IOException e) {
// catch possible io errors from readLine()
System.out.println("Uh oh, got an IOException error!");
e.printStackTrace();
}
List l = Arrays.asList(original);
Collections.shuffle(l);
System.out.println(l);
} // end of readMyFile()
} // end of class
Ed Byrnes
Ranch Hand

Joined: Feb 04, 2001
Posts: 33
Now the program counts the number of records and uses that number to specify the size of the array original[]. It is getting closer to what I want it to do. It prints the original list of names and then a shuffled form. It is a rough program.
// FileReadTest.java
// Copyright 1998 DevDaily Interactive, Inc. All Rights Reserved.
import java.io.*;
import java.util.*;
class FileReadTest1 {
//--------------------------------------------------< main >--------//
public static void main (String[] args) {
FileReadTest1 t = new FileReadTest1();
t.readMyFile();
}

//--------------------------------------------< readMyFile >--------//
void readMyFile() {
String record1 = null;
String record = null;
int recCount = 0;
int recCount1 = 0;

try //get number of records
{
FileReader fr1 = new FileReader("mydata.txt");
BufferedReader br1 = new BufferedReader(fr1);
record1 = new String();
while ((record1 = br1.readLine()) != null) {
recCount1++ ;
}
}catch (IOException e)
{
// catch possible io errors from readLine()
System.out.println("Uh oh, got an IOException error!");
e.printStackTrace();
}
String[] original = new String[recCount1];

try
{
FileReader fr = new FileReader("mydata.txt");
BufferedReader br = new BufferedReader(fr);
record = new String();
while ((record = br.readLine()) != null) {
original[recCount] = new String(record);
recCount++ ;
System.out.println(record);
}
}catch (IOException e)
{
// catch possible io errors from readLine()
System.out.println("Uh oh, got an IOException error!");
e.printStackTrace();
}
List l = Arrays.asList(original);
Collections.shuffle(l);
System.out.println(l);
} // end of readMyFile()
} // end of class
Ed Byrnes
Ranch Hand

Joined: Feb 04, 2001
Posts: 33
The following version reads a file called MyNames.txt which is a list of first names in my example. Each name is on a single line in MyNames.txt. The program gets the list and puts it into an array called original[]. System.arraycopy is used to make a copy of original[] called originalx[]. The elements in originalx are then shuffled. The output is formatted with column one being the original names and column two being the shuffled names. The program still needs a lot of work, but it is getting closer.
// FileReadTest1.java Ed Byrnes

import java.io.*;
import java.util.*;
import java.lang.*;
class FileReadTest1
{
public static void main (String[] args) {
FileReadTest1 t = new FileReadTest1();
t.readMyFile();
}
void readMyFile()
{
String record1 = null;
String record = null;
int recCount = 0;
int recCount1 = 0;
int recLength = 0;
try //get number of records as recCount1
{
FileReader fr1 = new FileReader("MyNames.txt");
BufferedReader br1 = new BufferedReader(fr1);
record1 = new String();
while ((record1 = br1.readLine()) != null)
{
recCount1++ ;
if ( record1.length() > recLength )
{
recLength = record1.length();
}

}
}catch (IOException e)
{
// catch possible io errors from readLine()
System.out.println("Uh oh, got an IOException error!");
e.printStackTrace();
}
String[] original = new String[recCount1];
String[] originalx = new String[recCount1];
try
{
FileReader fr = new FileReader("MyNames.txt");
BufferedReader br = new BufferedReader(fr);
record = new String();
while ((record = br.readLine()) != null)
{
original[recCount] = new String(record);
recCount++ ;
}
}catch (IOException e)
{
// catch possible io errors from readLine()
System.out.println("Uh oh, got an IOException error!");
e.printStackTrace();
}
System.arraycopy(original, 0, originalx, 0, recCount1);

String[] newNames = new String[ recCount1 ];
List l = Arrays.asList(original);
Collections.shuffle(l);
l.toArray(newNames);
for (int i = 0; i < newNames.length; i++ )
{
int spaces = recLength + 3 ;
int spaces2 = spaces - originalx[i].length();
String spaceL = "";
for (int j = 0; j<spaces2; j++)>
{
spaceL += " ";
}
System.out.println(originalx[i] + spaceL + newNames[i] );
}
} // end of readMyFile()
} // end of class
Chris Stehno
Ranch Hand

Joined: Feb 26, 2001
Posts: 180
Maybe I am missing something from your posts (I skimmed a little, sorry) ... looks like you are taking the long way around... hope this helps:
import java.io.*;
import java.util.*;
public class Test {
public static void main(String[] args){
try {
BufferedReader reader = new BufferedReader(new FileReader(new File(args[0])));
List list = new ArrayList();
String line = reader.readLine();
while(line != null){
list.add(line);
System.out.println(line);
line = reader.readLine();
}
reader.close();
String[] records = (String[])list.toArray(new String[]{""});

// your records are in the array one line per element
// also, each was printed to stout as it was read
} catch(Exception ex){
System.out.println(ex);
}
}
}


------------------
Chris Stehno (Sun Certified Programmer for the Java 2 Platform)


- Chris Stehno, SCPJ
Ed Byrnes
Ranch Hand

Joined: Feb 04, 2001
Posts: 33
Thanks Chris, it looks like much tighter code. I will have to fool around with it a bit to see how it works. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: text file into an array
 
Similar Threads
Create .txt file
BufferedReader.read
Writing to File
help with the code........
input file and output file