Text File reading

hey guys, need a little bit of help here... Im reading from a text file a certain number of characters.. I plan on passing it into an array but the problem is i have to initialize the array.. the size of my array depends on the number of characters in the text file.. so it could be any number.. so how should i go about with that?

thanks in advance!

-Arch-

[376 byte] By [Arch_Bytesa] at [2007-11-26 18:53:32]
# 1

If the file uses a one-byte-per-char character encoding, you could just use java.io.File to get the length of the file in bytes.

Or you could read the chars into a java.util.List and convert to an array later (if you really need to).This is only an option if you're using JDK1.5 and above; otherwise, I believe there are some Collections behave-alike classes provided on jakarta.apache.org.

paulcwa at 2007-7-9 6:27:33 > top of Java-index,Java Essentials,New To Java...
# 2
archy, checkout the File class... doesn't it have a length() or a size() or something. keith.
corlettka at 2007-7-9 6:27:33 > top of Java-index,Java Essentials,New To Java...
# 3

thanks guys.. i figured out a way around it.. now i have a new problem.. the object of this program is to throw an exception if a non-alphabetic character is encountered in the text file.. this is what i got so far.. it works if the non alphabetic character is the last character(ex. qwerty1) but if something follows it(ex.qwerty1iop) it will just print the whole thing and still print the exception message..

here's my code..

public class FileReading {

public static void main(String args[]) throws IOException{

String s;

System.out.println("Inside the TextFile:");

try {

FileReader in = new FileReader("ReadFromHere.txt");

BufferedReader br = new BufferedReader(in);

while ((s = br.readLine()) != null)

{System.out.println(s);

in.close();}

}

catch (IOException e) {

System.out.println("Non-Alphabetic Character encountered.. Program will terminate!");

}

}

}

Arch_Bytesa at 2007-7-9 6:27:33 > top of Java-index,Java Essentials,New To Java...
# 4

When you post code, please wrap it in [code][/code] tags so it's easy to read.

I don't see anything in your code that checks whether there are printing characters in the line. Apparently your code reads and prints the first line, closes the input, then tries to read and print the second line, but since it's closed presumably it throws the IOException.

I don't see why you think that this checks for non-alpha characters. Unless maybe the input just happens to match the way this fails, and you're misinterpreting what's going on?

If you want to check for the types of characters, check out the java.lang.Character class. It has a bunch of methods, with names starting with "is", that tell you what kind of character you're looking at.

paulcwa at 2007-7-9 6:27:33 > top of Java-index,Java Essentials,New To Java...
# 5
yeah.. apparently that was the answer i was looking for.. the class that can tell what type of character it is..^_^ thanks ^_^
Arch_Bytesa at 2007-7-9 6:27:33 > top of Java-index,Java Essentials,New To Java...
# 6
Take a look at the charAt() method and for use a loop to parse chars. As Paul mentioned your code if flawed. You need to remove in.close() from inside the while loop as it closes each time you complete one iteration. Why not put it in a finally block?
kikemellya at 2007-7-9 6:27:33 > top of Java-index,Java Essentials,New To Java...
# 7

i've fixed my code.. and its running well.. now all i have to do is count the number of times a specific character appears.. any suggesetions?

public class FilePackages {

public static void main(String args[]) {

char s = 'a';

int count = 0;

FileReader in = null;

System.out.println("Inside the TextFile:");

try {

int i = 0;

in = new FileReader("ReadFromHere.txt");

BufferedReader br = new BufferedReader(in);

while ((i = br.read()) != -1)

{

if((i >= 65 && i <= 90)||(i>=97 && i<=122))

{ s = (char)i;

System.out.println(s);

count++;}

else

throw new IOException();

}in.close();

}

catch (IOException e) {

System.out.println("Non-Alphabetic Character encountered.. Program will terminate!");

System.out.println("Total number of valid characters is: " +count);

}

}

}

Arch_Bytesa at 2007-7-9 6:27:33 > top of Java-index,Java Essentials,New To Java...
# 8

I suggest like someone else already said, to store the chars in a List. This way once you're done reading the file you can do whatever you want with the List of chars.

However if you know what char to look for before beginning to read the file, you could just increment a counter in the while loop eg if (s == charToLookFor) counter++;

ErikSilkensena at 2007-7-9 6:27:33 > top of Java-index,Java Essentials,New To Java...
# 9
But the ArrayList only accepts objects so it wont capture the character arguements..? is there a way to convert it to an object?
Arch_Bytesa at 2007-7-9 6:27:33 > top of Java-index,Java Essentials,New To Java...
# 10
You can use java.lang.Character objects. If you're using JDK 1.5, I believe the conversion is done automatically.
paulcwa at 2007-7-9 6:27:33 > top of Java-index,Java Essentials,New To Java...
# 11
thanks!!! got it!!! ^_^
Arch_Bytesa at 2007-7-9 6:27:33 > top of Java-index,Java Essentials,New To Java...