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");
}
}
No comments:
Post a Comment