Results 1 to 4 of 4

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

  1. #1
    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.

  2. #2
    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

  3. #3
    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();
          }
        }
      }
    }

  4. #4
    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();
    }
    }
    }
    }

Similar Threads

  1. how to write this pyramid using java?
    By Rainy in forum Software Development
    Replies: 1
    Last Post: 07-09-2011, 02:30 PM
  2. Read and Write a .txt file with Java
    By Mulan in forum Software Development
    Replies: 4
    Last Post: 28-07-2010, 02:03 AM
  3. How to write string data to file using java program?
    By Linoo in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 09:26 PM
  4. File read-write mode in java
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 10:02 AM
  5. How to write to file java using bufferedwriter?
    By Penzoil in forum Software Development
    Replies: 3
    Last Post: 28-07-2009, 05:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,754,104.13917 seconds with 16 queries