473,385 Members | 1,282 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,385 software developers and data experts.

How to read a txt file with ObjectInputStream

I am trying to read a txt file with ObjectInputStream, but it is throwing IOException, the "exc" in the code below shows java.io.StreamCorruptedException, please I need quick help for this. Thank you in advance.
Expand|Select|Wrap|Line Numbers
  1.  catch(IOException exc) 
Jul 28 '10 #1
3 2726
chaarmann
785 Expert 512MB
An ObjectInputStream loads objects and therefore the corresponding file contains binary data! But a text file contains text and no binary data.
If you want to read a text file, use FileInputStream()
Jul 30 '10 #2
So how could I read a line of text with FileInputReader?
Jul 30 '10 #3
chaarmann
785 Expert 512MB
You want to load the text inside the file and store it as a string in memory.
Try to write your own load methods. By doing so, you'll learn a lot. If you are stuck, list it here what you have done so far, and I will correct it or will give you more hints or will show you the code that solves your problem. It's really not a big task, my code size for reading a file into a string consists of only 7 lines.

Hwo to do:
For reading a file, you should make a re-usabel method:
"public static String readFileAsString(String sourceFileName) throws IOException"
This method contains source code that uses following classes:
- class File, methods: File(filename), exists()
- class FileInputStream, methods: FileInputStream(name), available(), read (byte[] data)
- class String, methods: new(byte[] data)
- class IOException, methods: IOException(errorMessage)

So read about all these classes and the listed methods in the javadocs. That's all you need for assembling the desired method.
Jul 31 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: domtam | last post by:
Suppose I have one of those USB storage devices (like a mp3 player, USB thumbdrive, or even digital camera) connected to my computer. I'd like to write a C# program that can - detect that the...
40
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
2
by: Profetas | last post by:
I have the following code that detects a <c> and </c> #include <stdio.h> main(int argc, char *argv) { FILE* fp; char data;
3
by: BobAchgill | last post by:
I am trying to read in a xml file that I exported from my MSAccess table using the following lines of code but it bombs. Is this the right code? Is there a way to get a clean export from Access...
2
by: somequestion | last post by:
During copying file , wanna read file Size like this string CheckFileSize(string fileName) { if( fileName == null ) return; FileInfo fi = new FileInfo(fileName); return fi.Length.ToString();...
1
by: potluri040 | last post by:
hi, could any one let me know how to read file thru VB script. Scenerio is like this: i am keeping a personal details such as name, age, sex, company, location in a text file called user details...
1
by: samira | last post by:
hi all, i built aproject to my client he need that the visitors to the site can read file from his computer, how can i do this , we uuse asp.net over c#
3
by: =?Utf-8?B?Sm9obiBXYWxrZXI=?= | last post by:
Hi, Is there anything wrong with the code below in sending my browser page to Excel? Before my page opens in Excel there's a message "Problems came up in the following areas during load:" and it...
2
by: xplode144 | last post by:
I have a Web application. i need to read a file once during the startup and preserve the read data throughout the life of the application. i will to access the data often during the page_load of...
2
by: danimian | last post by:
Hello, first i am creating xml file if file does not exist. String myFile = "C:\myxmlfile.xml"; if (!File.Exists(myFile)) { using (FileStream conStream = new FileStream(myFile,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.