473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I read a string from a file?

I'm trying to read a file that contains text for a decimal count, convert it
to binary, increment and write it back out. I'm using Java 2 in SuSE 8.2,
installed from the SuSE CD's.

When using FileReader the values that are read in are the ASCII values of
the decimal count, formatted as two bytes for each original single ASCII
byte. Converting it to an integer results in a double digit value for each
original single digit value:

26 Original value in file - created using a text editor.
5054 The value that Java reports when using System.out.println();
to print the string value (assumed to be but not really!) or
the string converted to an integer value.

Trying other methods found on the net or in books results in the method not
found or deprecated.

And while the compiler will tell me it's deprecated, I can't find any
information about what has replaced the deprecated methods.

Thanks, John
Jul 17 '05 #1
2 41721
import java.io.*;
public class test {
public static void main(String args[]) {
String line;
double value;
BufferedReader in;
try {
in = new BufferedReader(new FileReader("MyFile.txt"));
line = in.readLine();
value = Double.parseDouble(line);
value++;
in.close();

File f = new File("MyFile.txt");
PrintWriter out = new PrintWriter(new FileWriter(f));
out.println(value);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
--------------------------------------------------------------------------
echo 12.4 > MyFile.txt
javac test.java
java test
cat MyFile.txt
--------------------------------------------------------------------------

"John Bowling" <jo*****************@cox.net> wrote in message news:qT7cb.8104$vj2.1302@fed1read06...
I'm trying to read a file that contains text for a decimal count, convert it
to binary, increment and write it back out. I'm using Java 2 in SuSE 8.2,
installed from the SuSE CD's.

When using FileReader the values that are read in are the ASCII values of
the decimal count, formatted as two bytes for each original single ASCII
byte. Converting it to an integer results in a double digit value for each
original single digit value:

26 Original value in file - created using a text editor.
5054 The value that Java reports when using System.out.println();
to print the string value (assumed to be but not really!) or
the string converted to an integer value.

Trying other methods found on the net or in books results in the method not
found or deprecated.

And while the compiler will tell me it's deprecated, I can't find any
information about what has replaced the deprecated methods.

Thanks, John

Jul 17 '05 #2
I just tested it and it worked perfectly.
Thanks, John

"Phil..." <ry***@ieee.org> wrote in message
news:Hx*******************@rwcrnsc51.ops.asp.att.n et...
import java.io.*;
public class test {
public static void main(String args[]) {
String line;
double value;
BufferedReader in;
try {
in = new BufferedReader(new FileReader("MyFile.txt"));
line = in.readLine();
value = Double.parseDouble(line);
value++;
in.close();

File f = new File("MyFile.txt");
PrintWriter out = new PrintWriter(new FileWriter(f));
out.println(value);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
--------------------------------------------------------------------------
echo 12.4 > MyFile.txt
javac test.java
java test
cat MyFile.txt
--------------------------------------------------------------------------

"John Bowling" <jo*****************@cox.net> wrote in message
news:qT7cb.8104$vj2.1302@fed1read06...
I'm trying to read a file that contains text for a decimal count, convert it to binary, increment and write it back out. I'm using Java 2 in SuSE 8.2,
installed from the SuSE CD's.

When using FileReader the values that are read in are the ASCII values of
the decimal count, formatted as two bytes for each original single ASCII
byte. Converting it to an integer results in a double digit value for each original single digit value:

26 Original value in file - created using a text editor.
5054 The value that Java reports when using System.out.println();
to print the string value (assumed to be but not really!) or the string converted to an integer value.

Trying other methods found on the net or in books results in the method not found or deprecated.

And while the compiler will tell me it's deprecated, I can't find any
information about what has replaced the deprecated methods.

Thanks, John

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: _mario.lat | last post by:
I have a file whith four field in each line: string, int, int, float. For example: age 3 5 3.5 avr 6 7 3.6 .... .... how can I read this file doing something like that:
2
by: Raghu Gupta | last post by:
Hi, Actually i am facing some problem using quickbooks and presently i am not so expert in using it. I am using c# code to retrive the data from quickbooks. it doesn't matter which language we...
31
by: microsoft.news.com | last post by:
I need to read a text file but I need to start on line 2 of the file, How can I start reading the text file starting with the second line and not the first line in the file?
15
by: ruca | last post by:
Hi, Can I read a .TXT File to a DataSet? How can I do that? I want to read his lines to a DropDownList. This lines are the names of employees that I export from an application that I have. I...
5
by: Piotr | last post by:
I have a qestion about reading file in C++. How can I read a file line by line (i.e. putting a line ends wtih \n in a string) I tried the following, it does read the file, but it does not read...
3
by: John | last post by:
I have a text file that is formatted like this: 123456789 SOLD 1 12/12/2006 Smith 45,525 987654321 SOLD 3 01/01/2006 Jones 23,252
1
by: Quinn | last post by:
Hi all, I have some binary files in the following format: text line 1 text line 2 .... text line N end of text single in binary 1 single in binary 2 single N EOF
6
by: portCo | last post by:
Hello there, I am creating a vb application which is some like like a questionare. Application read a text file which contains many questions and display one question and the input is needed...
7
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, I am using visual studio C# window and I have an xml file which I need to read that file using Object Oriented program. What codes I should use in my class to load and read that xml file....
4
by: Mr Gray | last post by:
Hi Guys, My requirement is to scan an FTP directory for the presence of 4 files and if a specific file exists I can begin to GET those files, read the contents and save the contents into an SQL...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.