Miletone

Rp0 / Rp1.000.000

Monday, January 11, 2021

[JAVA] MEMBUAT FILE TEXT (.txt) dengan isinya.

 import java.io.*;

import java.util.Scanner;

/**

 *

 * @author ANDI

 */

public class InputFile {

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

        /*File f = new File("D:\\stream.doc");

        FileOutputStream out;

        PrintStream p;

        try {

            out = new FileOutputStream("D:\\stream.doc");

            p = new PrintStream(out);

            p.println("Ini String yang di tulis ke file");

            p.println("bla bla bla");

            p.close();

        } catch (Exception e) {

            System.err.println("Error Writting to file");

        }*/

//attach keyboard to DataInputStream 

        DataInputStream dis=new DataInputStream(System.in); 

  

        // attach file to FileOutputStream 

        FileOutputStream fout=new FileOutputStream("file.txt"); 

  

        //attach FileOutputStream to BufferedOutputStream 

        BufferedOutputStream bout=new BufferedOutputStream(fout,1024); 

        System.out.println("Enter text (@ at the end):"); 

        char ch; 

  

        //read characters from dis into ch. Then write them into bout. 

        //repeat this as long as the read character is not @ 

        while((ch=(char)dis.read())!='@') 

        { 

            bout.write(ch); 

        } 

        //close the file 

        bout.close(); 

    }  

    }


No comments: