Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 03-08-2009
Member
 
Join Date: May 2008
Posts: 32
Need example of fileoutputstream out.write to a file in Java.

Hi,

Need example of fileoutputstream out.write to a file in Java.

Since I am getting "Red.java:6: unreported exception ava.io.FileNotFoundException; must be caught or declared to be thrown" error in my code.

Thanks in advance.
Reply With Quote
  #2  
Old 03-08-2009
Member
 
Join Date: May 2008
Posts: 44
Re: Need example of fileoutputstream out.write to a file in Java.

Code:
 import java.io.*;

public class WriteFile{

    public static void main(String[] args) throws IOException{

      File f=new File("textfile1.txt");
      FileOutputStream fop=new FileOutputStream(f);

      if(f.exists()){
      String str="This data is written through the program";
          fop.write(str.getBytes());

          fop.flush();
          fop.close();
          System.out.println("The data has been written");
          }

          else
            System.out.println("This file is not exist");
    }
  }
Output:

Code:
C:\ram>javac WriteFile.java

C:\ram>java WriteFile
The data has been written

C:\ram>
Explanation
Reply With Quote
  #3  
Old 03-08-2009
Member
 
Join Date: May 2008
Posts: 41
Re: Need example of fileoutputstream out.write to a file in Java.

Copy Bytes between FileInputStream and FileOutputStream

Code:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyBytes {
  public static void main(String[] args) throws IOException {
    FileInputStream in = null;
    FileOutputStream out = null;
    try {
      in = new FileInputStream("xanadu.txt");
      out = new FileOutputStream("outagain.txt");
      int c;

      while ((c = in.read()) != -1) {
        out.write(c);
      }

    } finally {
      if (in != null) {
        in.close();
      }
      if (out != null) {
        out.close();
      }
    }
  }
}
Reply With Quote
  #4  
Old 03-08-2009
Member
 
Join Date: May 2008
Posts: 20
Re: Need example of fileoutputstream out.write to a file in Java.

Here is a sample code:

Code:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteBytes {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
int j;
in = new FileInputStream("input.txt");
out = new FileOutputStream("output.txt");

while ((j = in.read()) != -1){
out.write(j);
}
}
finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Need example of fileoutputstream out.write to a file in Java."
Thread Thread Starter Forum Replies Last Post
how to write this pyramid using java? Rainy Software Development 1 07-09-2011 02:30 PM
Read and Write a .txt file with Java Mulan Software Development 4 28-07-2010 02:03 AM
How to write string data to file using java program? Linoo Software Development 5 21-01-2010 09:26 PM
File read-write mode in java ISAIAH Software Development 5 20-01-2010 10:02 AM
How to write to file java using bufferedwriter? Penzoil Software Development 3 28-07-2009 05:31 PM


All times are GMT +5.5. The time now is 03:02 AM.