Miletone

Rp0 / Rp1.000.000

Monday, January 11, 2021

[JAVA] HOW TO MAKE FILE (.txt) with filled.

 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(); 

    }  

    }


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

[JAVA] HOW TO MAKE FILE (UNCATHEGORY).

  import java.io.*;

import java.util.Scanner;

/**

 *

 * @author ANDI

 */

public class InputFile {

    public static void main (String [] args){

        /*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");

        }*/

try  

{  

Scanner sc=new Scanner(System.in);         //object of Scanner class  

System.out.print("Enter the file name: ");  

String name=sc.nextLine();              //variable name to store the file name  

FileOutputStream fos=new FileOutputStream(name, true);  // true for append mode  

System.out.print("Enter file content: ");         

String str=sc.nextLine()+"\n";      //str stores the string which we have entered  

byte[] b= str.getBytes();       //converts string into bytes  

fos.write(b);           //writes bytes into file  

fos.close();            //close the file  

System.out.println("file saved.");  

}  

catch(Exception e)  

{  

e.printStackTrace();          

}     System.out.println("Data yang anda input, selesai");

    }

}


[JAVA] MEMBUAT FILE (BELUM TERKATEGORI).

 import java.io.*;

import java.util.Scanner;

/**

 *

 * @author ANDI

 */

public class InputFile {

    public static void main (String [] args){

        /*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");

        }*/

try  

{  

Scanner sc=new Scanner(System.in);         //object of Scanner class  

System.out.print("Enter the file name: ");  

String name=sc.nextLine();              //variable name to store the file name  

FileOutputStream fos=new FileOutputStream(name, true);  // true for append mode  

System.out.print("Enter file content: ");         

String str=sc.nextLine()+"\n";      //str stores the string which we have entered  

byte[] b= str.getBytes();       //converts string into bytes  

fos.write(b);           //writes bytes into file  

fos.close();            //close the file  

System.out.println("file saved.");  

}  

catch(Exception e)  

{  

e.printStackTrace();          

}     System.out.println("Data yang anda input, selesai");

    }

}