Posted in Java 13 years ago 1 min read
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package LatihanString;
import java.io.BufferedWriter;
import java.io.FileWriter;
/**
*
* @author Azhar
*/
public class WriteTxt {
public static void main(String[] args) {
String teks = "Write file using Java";
String path = "output.txt";
try {
FileWriter fw = new FileWriter(path);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(teks);
bw.close();
} catch (Exception e) {
}
}
}
Sedangkan jika anda ingin menyimpan di direktori lain, anda dapat mengubah path dari source code tersebut menjadi seperti ini:
String path = C:\\FolderAnda\\output.txtPerhatikan bahwa path di atas menggunakan escape character yaitu backslash (\) agar direktori dapat terbaca dengan benar. Semoga bermanfaat.