LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2007, 01:40 PM   #1
chief_officer
Member
 
Registered: Mar 2006
Location: Istanbul, TR
Distribution: Red Hat, CentOS, Ubuntu
Posts: 181

Rep: Reputation: 30
Java: Reading characters from a text file


Friends,

I am totally confused...

I am trying to write a small Java program that reads data from a text file. So far, I am able to read the entire file, however I want to group the values in the text file and write with headers.

Step 1 - Here is my text file:
Quote:
MYTEXTFILE
1 XXXX1234567 LINMACWIN*
1 YYYY8901234 DOSOS2YOU*
Step 2 - The explanation of the text file:
  1. The first string (MYTEXTFILE) is an indicator that the file that will be opened is a native file of the application.
  2. The lines ending with * are individual lines.
  3. Each line has different data. From the sample file above:
    1: count, as integer
    XXXX1234567, YYYY8901234: IDs.
    LIN: property 1
    MAC: property 2
    WIN: property 3
    *: end of line character

In fact, all the lines ending with * are character arrays. Inside the arrays, the data are to be read by file specifications; e.g. 3 characters to count, 11 characters IDs, 3 spaces, 3 characters for property 1, 3 characters for property 2, 3 characters for property 3 etc. until *.

Code:
package textfileread;

import java.io.*;

public class Main {
    
    public Main() {
    }
    
    public static void main(String[] args) throws Exception
    {
  
    FileReader conts = new FileReader ("pmlfile.pml");
    BufferedReader br = new BufferedReader (conts);
    String s;
    
    while ((s=br.readLine())!=null)
    {
          
        System.out.println(s);
    }
    conts.close();
    }
These are the only lines that I could code. I am trying to insert CharArrayReader into the code to read characters from the text file, but somehow

Code:
CharArrayReader item_01 = new CharArrayReader(0,0,3)
gives me lots of errors.

I am googling for hours and I have come to a state that I am unable to understand what I read.

Can anyone give me a helping hand?

Regards.
 
Old 03-25-2007, 04:12 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
ive never used CharArrayReader, but from the API:
Quote:
CharArrayReader(char[] buf, int offset, int length)
and your passing 3 ints instead of char[], int, int.

after fixing this, post the _error messages_ you recieve so we can try to further help.
 
Old 03-25-2007, 04:13 PM   #3
rednuht
Member
 
Registered: Aug 2005
Posts: 239
Blog Entries: 1

Rep: Reputation: 31
its been a while since I used Java but this might help
Code:
                byte dataBuffer[];
		int amountRead=0;

		dataBuffer = new byte[VMIfileSize];
  		try {
      	      BufferedInputStream disi = null;
              disi = new BufferedInputStream(in);

      	      amountRead = (int)disi.read(dataBuffer,0,VMIfileSize);
I am of course refering to the read(buffer,start,end) call.
 
Old 03-26-2007, 11:28 AM   #4
chief_officer
Member
 
Registered: Mar 2006
Location: Istanbul, TR
Distribution: Red Hat, CentOS, Ubuntu
Posts: 181

Original Poster
Rep: Reputation: 30
@rednuht

Code:
package textfileread;

import java.io.*;

public class Main {
    
    public Main() {
    }
    
    public static void main(String[] args) throws Exception
    {
    int amount_read = 0;
   
    FileReader conts = new FileReader ("pmlfile.pml");
    BufferedReader br = new BufferedReader (conts);
    dataBuffer = new byte [VMIfileSize];

    
    while ((s=br.readLine())!=null)
    {
        BufferedInputStream disi = null;
        disi = new BufferedInputStream(in);
        amountread = (int)disi.read(databuffer,0,VMIfilesize);
    }
    conts.close();
    }
    
}
Quote:
deps-jar:
Compiling 1 source file to /home/diablo/java_codes/textfileread_01/build/classes
/home/diablo/java_codes/textfileread_01/src/textfileread_01/Main.java:26: cannot find symbol
symbol : variable dataBuffer
location: class textfileread.Main
dataBuffer = new byte [VMIfileSize];
/home/diablo/java_codes/textfileread_01/src/textfileread_01/Main.java:26: cannot find symbol
symbol : variable VMIfileSize
location: class textfileread.Main
dataBuffer = new byte [VMIfileSize];
/home/diablo/java_codes/textfileread_01/src/textfileread_01/Main.java:29: cannot find symbol
symbol : variable s
location: class textfileread.Main
while ((s=br.readLine())!=null)
/home/diablo/java_codes/textfileread_01/src/textfileread_01/Main.java:32: cannot find symbol
symbol : variable in
location: class textfileread.Main
disi = new BufferedInputStream(in);
/home/diablo/java_codes/textfileread_01/src/textfileread_01/Main.java:34: cannot find symbol
symbol : variable amountread
location: class textfileread.Main
amountread = (int)disi.read(databuffer,0,VMIfilesize);
/home/diablo/java_codes/textfileread_01/src/textfileread_01/Main.java:34: cannot find symbol
symbol : variable databuffer
location: class textfileread.Main
amountread = (int)disi.read(databuffer,0,VMIfilesize);
/home/diablo/java_codes/textfileread_01/src/textfileread_01/Main.java:34: cannot find symbol
symbol : variable VMIfilesize
location: class textfileread.Main
amountread = (int)disi.read(databuffer,0,VMIfilesize);
7 errors
BUILD FAILED (total time: 0 seconds)

@nadroj
Code:
package textfileread;

/**
 *
 * @author diablo
 */

import java.io.*;

public class Main {
    
    /** Creates a new instance of Main */
    public Main() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception
    {
    //int c[] = new char[cont_no];
    
    FileReader conts = new FileReader ("pmlfile.pml");
    BufferedReader br = new BufferedReader (conts);
    String s;
    
    while ((s=br.readLine())!=null)
    {
        BufferedInputStream disi = null;
        CharArrayReader input = new CharArrayReader(3, 1, 4);
        System.out.println(input);
    }
    conts.close();
    }
    
}
the error is:

Quote:
nit:
deps-jar:
Compiling 1 source file to /home/diablo/java_codes/textfileread_01/build/classes
/home/diablo/java_codes/textfileread_01/src/textfileread_01/Main.java:30: cannot find symbol
symbol : constructor CharArrayReader(int,int,int)
location: class java.io.CharArrayReader
CharArrayReader input = new CharArrayReader(3, 1, 3);
1 error
BUILD FAILED (total time: 0 seconds)
I am thinking that I am close, that was what I tried to the yesterday before posting. As per the API, I should be able to read 3 characters with CharArrayReader(3, 1, 4). However, I am getting the error above.

What is going wrong? Text files are the simplest files that can be read and written. Reading them should not be so hard.

 
Old 03-26-2007, 06:42 PM   #5
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Quote:
As per the API, I should be able to read 3 characters with CharArrayReader(3, 1, 4). However, I am getting the error above.
from the API page:
Quote:
Constructor Summary
..CharArrayReader(char[] buf)
............Create an CharArrayReader from the specified array of chars.
..CharArrayReader(char[] buf, int offset, int length)
............Create an CharArrayReader from the specified array of chars.
(i added the .'s to keep everything lined up).

you have to pass 'char[], int, int', not 'int,int,int'

EDIT:
regarding errors from your previous post directed to rednuht
- line 16:
Code:
dataBuffer = new byte [VMIfileSize];
do you mean to use ()s instead of []s? Also, where is VMIfileSize declared?

- line 19:
Code:
while ((s=br.readLine())!=null)
where is 's' declared?

- line 22:
Code:
disi = new BufferedInputStream(in);
where is 'in' declared?

- line 23:
Code:
amountread = (int)disi.read(databuffer,0,VMIfilesize);
'databuffer' isnt declared, do you mean 'dataBuffer'? remember java is cASe SENsiTIve.

Last edited by nadroj; 03-26-2007 at 06:51 PM.
 
Old 03-26-2007, 07:04 PM   #6
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Might I suggest just using a different input class altogether?
I make *almost* exclusive use of java.util.Scanner in every Java application I write.

Code:
import java.util.Scanner;
public class Blarg {
  // To read in from a file:
  Scanner inStream = new Scanner(new File("/path/to/file"));
  ArrayList buf = new ArrayList();

  // To read in from user-typed responses:
  Scanner userInput = new Scanner(System.in);
  String input;
  public static void main(String[] args) {
    while (inStream.hasNext() == true) buf.add(inStream.nextLine());
    System.out.print("Display contents of file (y/n)? ");
    input = userInput.nextLine();
    if (input.equalsIgnoreCase("y")) {
      for (int i = 0; i < buf.size(); i++) {
        System.out.println(buf.get(i).toString());
      }
    } else System.exit(0);
  }
}


Now, the Scanner.nextLine() and Scanner.next() methods return strings. If you want chars, use the following:
Code:
char c = Scanner.findInLine(".").charAt(0);
Which, literally, means find character . at element 0 in the string. Think of the string as an array; the period, ".", is treated as a wildcard character.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving a text file as a variable and reading it every second in java script mrobertson Programming 4 03-26-2007 08:25 PM
Java: Reading from a text file slow on Tomcat robbbert Programming 7 08-25-2006 12:07 PM
Clearing control characters from a text file LinuxLala Linux - General 1 04-07-2006 06:45 AM
Convert special characters in text file nyk Linux - Software 1 01-05-2005 03:20 PM
inserting/deleting characters into a text file ananthbv Programming 7 07-13-2004 11:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration