Created
May 4, 2019 14:06
-
-
Save leanhboi1999/7785ae901ae4781971602edab002ea12 to your computer and use it in GitHub Desktop.
InOutFileOnJava
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package learn.io; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.util.ArrayList; | |
import learn.model.KhachHang; | |
public class FileFactory { | |
public static boolean luuFile(ArrayList<KhachHang> dskh, String path) { | |
try { | |
FileOutputStream fos = new FileOutputStream(path); | |
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); | |
BufferedWriter bw = new BufferedWriter(osw); | |
for (KhachHang kh : dskh) { | |
String line = kh.getMa() + ";" + kh.getTen(); | |
bw.write(line); | |
bw.newLine(); | |
} | |
bw.close(); | |
osw.close(); | |
fos.close(); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
return true; | |
} | |
public static ArrayList<KhachHang> docFile(String path) { | |
ArrayList<KhachHang> dskh = new ArrayList<KhachHang>(); | |
try { | |
FileInputStream fis = new FileInputStream(path); | |
InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); | |
BufferedReader br = new BufferedReader(isr); | |
String line = br.readLine(); | |
while (line != null) { | |
String[] arr = line.split(";"); | |
if (arr.length == 2) { | |
KhachHang kh = new KhachHang(arr[0], arr[1]); | |
dskh.add(kh); | |
} | |
line = br.readLine(); | |
} | |
br.close(); | |
isr.close(); | |
fis.close(); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
return dskh; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment