ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
The first string (MYTEXTFILE) is an indicator that the file that will be opened is a native file of the application.
The lines ending with * are individual lines.
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.
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.