Archive

Category Archives for "Java File"

Sử dụng thư viện NIO để đọc ghi file trong Java

1. Đọc FileTạo class ReadFileWithJavaNIO.java package com.loop.io;import java.io.File;import java.io.IOException;import java.nio.file.Files;import java.util.ArrayList;public class ReadFileWithJavaNIO {  /**  * Create by Cuder  *  * @throws IOException  */  public static void main(String[] args) throws IOException {    File f = new File(“resource/textFile.txt”);    ArrayList<String> res = (ArrayList<String>) Files.readAllLines(f    .toPath());    for (int i = 0; i < res.size(); i++) {      System.out.println(res.get(i));    }  }} Kết quả Line 1 Line 2 Line 3 Line 4 Line 5 2. […]

Xem chi tiết

Sử dụng BufferedWriter để ghi file trong Java

1. Ghi file cơ bản sử dụng java BufferedWriterTạo class WriteNewFileWithBufferedWriter.java package com.loop.io;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;public class WriteNewFileWithBufferedWriter {  /**  *  * Create by Cuder  */  public static void main(String[] args) {    String outputFile = “resource/fileWrite1.txt”;    BufferedWriter bw = null;    FileWriter fw = null;    try {      fw = new FileWriter(outputFile);      bw = new BufferedWriter(fw);      for (int i = 0; i < 10; i++) {        bw.write(“line content : ” […]

Xem chi tiết

Sử dụng OutputStreamWriter để ghi file trong Java

1. Ghi file cơ bản sử dụng java OutputStreamWriterTạo class WriteNewFileWIthOutputStreamWriter.java package com.loop.io;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;public class WriteNewFileWIthOutputStreamWriter {  /**  *  * Create by Cuder  */  public static void main(String[] args) throws IOException {    FileOutputStream fout = null;    OutputStreamWriter out = null;    try {      fout = new FileOutputStream(“resource/fileWrite3.txt”);      out = new OutputStreamWriter(fout, “UTF-8”);      for (int i = 0; i < 10; i++) {        out.write(“line content : ” […]

Xem chi tiết

Sử dụng FileOutputStream để ghi file trong Java

1. Ghi file cơ bản sử dụng java FileOutputStreamTạo class WriteNewFileWIthFileOutputStream .java package com.loop.io;import java.io.FileOutputStream;import java.io.IOException;public class WriteNewFileWIthFileOutputStream {  /**  *  * Create by Cuder  *  */  public static void main(String[] args) throws IOException {    FileOutputStream fout = null;    try {      fout = new FileOutputStream(“resource/fileWrite5.txt”);      for (int i = 0; i < 10; i++) {        fout.write((“line content : ” + (i + 1) + “\n”).getBytes());      }    } catch […]

Xem chi tiết

Sử dụng InputStreamReader để đọc file trong Java

1. Đọc file cơ bản sử dụng java InputStreamReaderNội dung file textFile.txt Line 1Line 2Line 3Line 4Line 5 Tạo class ReadFileWIthInputStreamReader.java package com.loop.io;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;public class ReadFileWIthInputStreamReader {  /**  *  * Create by Cuder  */  public static void main(String[] args) throws IOException {    FileInputStream fin = null;    InputStreamReader in = null;    try {      fin = new FileInputStream(“resource/textFile.txt”);      in = new InputStreamReader(fin, “UTF8”);      int contents;      while ((contents […]

Xem chi tiết

Sử dụng FileInputStream để đọc file trong Java

Đọc file cơ bản sử dụng java FileInputStreamNội dung file text : Line 1Line 2Line 3Line 4Line 5 Tạo class ReadTextFile.java package com.loop.io;import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class ReadTextFile {  public static void main(String[] args) {    File file = new File(“resource/textFile.txt”);    FileInputStream fis = null;    try {      fis = new FileInputStream(file);      System.out.println(“Tổng số bytes : ” + fis.available());      int content;      while ((content = fis.read()) […]

Xem chi tiết