wood burning stoves 2.0
The moose likes I/O and Streams and the fly likes problem in reading and writing a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "problem in reading and writing a file" Watch "problem in reading and writing a file" New topic

problem in reading and writing a file

naresh govindaswmay
Ranch Hand

Joined: Aug 15, 2007
Posts: 90

package com.test;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

* This program reads a text file line by line and print to the console. It uses
* FileOutputStream to read the file.
public class FileInput {

public static void main(String[] args) {

File file = new File("C:\\MyFile.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;

try {

fis = new FileInputStream(file);

// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);

// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {

// this statement reads the line from the file and print it to
// the console.


// dispose all the resources after using them.

} catch (FileNotFoundException e) {
} catch (IOException e) {
This is for Reading a file.

package com.test;
import java.io.*;

public class FileOutput {

public static void main(String[] args) {

FileOutputStream fos;
DataOutputStream dos;

try {
File file= new File("C:\\MyFile.txt");
fos = new FileOutputStream(file);
dos=new DataOutputStream(fos);

catch (IOException e)


This is for writing a file.I can able to read and write contents.after executing the writing file it will updates some contents in that txt file that time i opened that txt file i am not able to see that character in which i updated.I saw some junk character.same thing happen in running the read program.please anny body help me out of this..

Thanks and Regards<br />Naresh govindaswamy
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

DataOutputStream does not write plain text files. It writes files for consumption by DataInputStream, so the files may not be readable in a text editor.
If you want a plain text file, use some sort of Reader and Writer combination.

[How To Ask Questions On JavaRanch]
I agree. Here's the link: http://aspose.com/file-tools
subject: problem in reading and writing a file
jQuery in Action, 3rd edition