By GokiSoft.com|
10:09 07/09/2021|
Java Advanced
[Share Code] Tìm hiểu File - FileInputStream & FileOutputStream - BufferedInputStream & BufferedOutStream - ObjectInputStream & ObjectOutputStream - Lập trình Java nâng cao - C2009G
#Student.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson03;
import java.io.Serializable;
/**
*
* @author Diep.Tran
*/
public class Student implements Serializable{
String fullname, email, address;
public Student() {
}
public Student(String fullname, String email, String address) {
this.fullname = fullname;
this.email = email;
this.address = address;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" + "fullname=" + fullname + ", email=" + email + ", address=" + address + "}\n";
}
public String getFileLine() {
return fullname + "," + email + "," + address + "\n";
}
public void parseFileLine(String line) {
try {
String[] params = line.split(",");
fullname = params[0];
email = params[1];
address = params[2];
} catch(Exception e) {}
}
}
#Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lesson03;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
//testFile();
//saveTextFile1();
//readTextFile1();
//readTextFile2();
//saveStudentIntoFile();
//readStudentFromFile();
//saveStudent();
readStudent();
}
static void readStudent() {
List<Student> studentList;
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("data/student.obj");
ois = new ObjectInputStream(fis);
studentList = (List<Student>) ois.readObject();
for (Student student : studentList) {
System.out.println(student);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(ois != null) {
try {
ois.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void saveStudent() {
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("A", "A@gmail.com", "Ha Noi"));
studentList.add(new Student("B", "B@gmail.com", "Nam Dinh"));
studentList.add(new Student("C", "C@gmail.com", "Ha Noi"));
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("data/student.obj");
oos = new ObjectOutputStream(fos);
oos.writeObject(studentList);
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(oos != null) {
try {
oos.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void readStudentFromFile() {
FileReader reader = null;
BufferedReader bufferedReader = null;
List<Student> studentList = new ArrayList<>();
try {
reader = new FileReader("data/student.txt");
bufferedReader = new BufferedReader(reader);
String line;
while((line = bufferedReader.readLine()) != null) {
if(line.isEmpty()) continue;
Student std = new Student();
std.parseFileLine(line);
studentList.add(std);
}
for (Student student : studentList) {
System.out.println(student);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(reader != null) {
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void saveStudentIntoFile() {
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("A", "A@gmail.com", "Ha Noi"));
studentList.add(new Student("B", "B@gmail.com", "Nam Dinh"));
studentList.add(new Student("C", "C@gmail.com", "Ha Noi"));
FileOutputStream fos = null;
Scanner scan = new Scanner(System.in);
try {
//Open connection to File
fos = new FileOutputStream("data/student.txt");
//For -> duyet tung sv.
//Fullname Email Address
for (Student student : studentList) {
String line = student.getFileLine();
byte[] data = line.getBytes("utf8");
fos.write(data);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void readTextFile2() {
FileReader reader = null;
BufferedReader bufferedReader = null;
StringBuilder builder = new StringBuilder();
try {
reader = new FileReader("data/f1.txt");
bufferedReader = new BufferedReader(reader);
String line;
while((line = bufferedReader.readLine()) != null) {
builder.append(line + "\n");
}
System.out.println(builder.toString());
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(reader != null) {
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void readTextFile1() {
FileInputStream fis = null;
StringBuilder builder = new StringBuilder();
try {
fis = new FileInputStream("data/f1.txt");
int code;
while((code = fis.read()) != -1) {
builder.append((char) code);
}
System.out.println(builder.toString());
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void saveTextFile1() {
FileOutputStream fos = null;
Scanner scan = new Scanner(System.in);
try {
//Open connection to File
fos = new FileOutputStream("data/f1.txt", true);
//Read/Write data.
//C1. Luu 1 String to File
//String str = "\nSinh vien lop C2009G - Aptech 54 Le Thanh Nghi";
//byte[] data = str.getBytes("utf8");
//fos.write(data);
//C2. Luu String dc nhap tu ban phim
String line;
do {
System.out.println("Nhap chuoi can them vao File: ");
line = scan.nextLine() + "\n";
byte[] data = line.getBytes("utf8");
fos.write(data);
System.out.println("Ban muon tiep tuc khong Y/n? ");
line = scan.nextLine();
} while (!line.equalsIgnoreCase("N"));
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void testFile() {
File file = new File("data");
File f2 = new File("data/test.txt");
if(file.isDirectory()) {
System.out.println("Folder");
} else {
System.out.println("File");
}
if(f2.exists()) {
System.out.println("File is existing");
} else {
System.out.println("Not found");
try {
f2.createNewFile();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println(file.getAbsolutePath());
try {
System.out.println(file.getCanonicalPath());
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
//Duyet folder
File f3 = new File("./");
File[] list = f3.listFiles();
for (File f : list) {
if(f.isFile()) {
System.out.println("File > " + f.getName());
} else {
System.out.println("> Folder: " + f.getName());
}
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)