Reading lines of a text file into a string array
Hi Guys,
I currently have this code
and what the code does above is basically reads in a text file line by line and stores all its contents in a sing String named text.
What i want it to do is it to store each line into an undefinied string array however im getting no where. Do you guys have any suggestion of how i can alter the code above to have each line stored in a seperat part of the array for example
test[2] = "the quick brown....."
thankyou for all your help
Re: Reading lines of a text file into a string array
You will either need to declare the size of the array beforehand or use something like an ArrayList
http://www.javaprogrammingforums.com...arraylist.html
Re: Reading lines of a text file into a string array
What about to store it in a Vector
like this....
Code Java:
import java.util.Vector;
public static void ReadFile2String(String InputFile)
{
try{
FileInputStream fstream = new FileInputStream(InputFile);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
text = "";
Vector myVector = new Vector();
while ((strLine = br.readLine()) != null) //loop through each line
{
myVector.add(strLine );
// Print the content on the console
text +=(strLine+"\n"); // Store the text file in the string
}
in.close();//Close the input stream
int i=0;
for(i=0;i<myVector.size();i++){
System.out.println("Position "+ i+" "+myVector.elementAt(i));
}
}
catch (Exception e)
{//Catch exception if any
System.out.println("Error: " + e.getMessage());
}
}
}
Re: Reading lines of a text file into a string array
Have a look at the Java class StringBuilder.
I beleive this will help you with what you are trying to achive.
When you have created an instance of StringBuilder use the append method to append your line to the string and if you want a new line after each line of text just do this.
mystringbuilder.append(lineFromFile + "\n");
Then when you are done you can use mystringbuilder.toString(); to return the entire string.
I forgot to mention check this link for the JavaDoc's on StringBuilder StringBuilder (Java 2 Platform SE 5.0)
Enjoy :o)
Re: Reading lines of a text file into a string array
Quote:
Originally Posted by
generalitico
What about to store it in a Vector
like this....
Code Java:
import java.util.Vector;
public static void ReadFile2String(String InputFile)
{
try{
FileInputStream fstream = new FileInputStream(InputFile);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
text = "";
Vector myVector = new Vector();
while ((strLine = br.readLine()) != null) //loop through each line
{
myVector.add(strLine );
// Print the content on the console
text +=(strLine+"\n"); // Store the text file in the string
}
in.close();//Close the input stream
int i=0;
for(i=0;i<myVector.size();i++){
System.out.println("Position "+ i+" "+myVector.elementAt(i));
}
}
catch (Exception e)
{//Catch exception if any
System.out.println("Error: " + e.getMessage());
}
}
}
More of a pet-peeve than anything else, but usually when you declare a List (you have used a Vector in this case) you want to specify what type of Objects are in that list. This is just a way to make it easier to access the Objects, as you will not need to cast the objects when you get them. This is not as much of an issue when you use Lists of Strings, as they are easy to cast since their Object Representation is just a String of its value (sort of odd, and I'm unsure how to explain it).
However, this does become a problem if you had a Vector of Objects other than Strings. For example, a Vector of JFrames.
If you declared your Vector using the way you did: Vector myVector = new Vector();, you would access the JFrames by saying:
Alternatively, if you declared your Vector to be restricted to only a Vector of JFrames like this: Vector<JFrame> myVector = new Vector<JFrame>();, you would access the JFrames by saying:
Code java:
JFrame frame = myVector.get(0);
So, the more acceptable way of creating your Vector above (acceptable meaning the general consensus of the programming community) would be saying:
Code java:
Vector<String> myVector = new Vector<String>();
Once again, sort of a moot point when you have a Vector of Strings, but it is good advise to have on the back-burner for your future data structures. Tell me if I am unclear.