LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Read text files (https://www.linuxquestions.org/questions/programming-9/read-text-files-170387/)

alanlow 04-15-2004 04:21 AM

Read text files
 
hi!! i am trying to write a software with Java 2 which require me to get infomation from a txt file and do some computation..

Example of text file:
NOAA 9
1 15427U 84123A 04105.28666882 .00000305 00000-0 17871-3 0 3273
2 15427 98.6074 164.8044 0015862 96.3400 263.9646 14.15133964997644
NOAA 10
1 16969U 86073A 04105.42065265 -.00000080 00000-0 -14183-4 0 2124
2 16969 98.7544 104.3853 0011689 304.4644 55.5432 14.27188008913932



My software must be able to detect NOAA 10 and do computation with the values classified under NOAA 10..

try{
FileReader file = new FileReader("Tles.txt");
BufferedReader buff = new BufferedReader(file);
boolean eof = false;
while (!eof)
{
String line = buff.readline();
if (line == null)
eof = true;
else
{
if( line == "NOAA 10")
perform calculation;
}
}
}catch (IOException e){
System.out.println("Error - -" +e.toString());
}


Thanks!!!!

pycoucou 04-15-2004 10:07 AM

This is a function I wrote a long time ago about ppm image reading (ie ascii images )

I use a CharArrayWriterGetbuf to get the data and a BufferManipulator

public void read() throws IOException {
int c;
int i; // colonne
int j; // ligne
int[] results;
int beforeDynamique = 0;
int[][] pixels;

ImageBNW_ppm image = this;

CharArrayWriterGetbuf data = new CharArrayWriterGetbuf();

There I read the file, ie I transfer to a Buffer

while ((c = fr_inputFile.read()) != -1) {
data.write(c);
}

I manipulate the buffer

BufferManipulator buf = new BufferManipulator(data.getBuf());

I get the header (entete in French ;-) )
ct_entete = buf.getCharData (0, '\n');

beforeDynamique = image.getDimension(buf);

results = buf.getIntData(beforeDynamique, '\n');

super.setDynamique(results[0]);

I read the pixels array

pixels = new int[super.getWidth()][super.getHeight()];

for (j=0; j<super.getHeight(); j++) {
for (i=0; i<super.getWidth(); i++) {
getData read depending up to the last character, \n in this example
results = buf.getIntData(results[1], '\n');
pixels[i][j] = results[0];
}
}

Junk stuff for my application

image.mc_pixels = new MatrixCalcul(pixels, super.getWidth(), super.getHeight());

data.close();

fr_inputFile.close();

}

I hope it was helpful. I don't remember exactrly everything but it might give you some idea and some classes to try. If you want further details, I could send you the classes to open an image for you to play with.

Cheers,
Pierre-Yves


All times are GMT -5. The time now is 07:00 AM.