By GokiSoft.com|
15:23 12/03/2021|
Java Advanced
[Share Code] Tìm hiểu về Generic & FileInputStream & FileOutputStream - Lập trình java nâng cao.
#Test1.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 aptech.java2.lession3;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class Test1 {
public static void main(String[] args) {
//Tim hieu ve class FILE
File file = new File("dist");
if(file.exists()) {
System.out.println("File exist");
} else {
System.out.println("Not exist");
try {
file.createNewFile();
} catch (IOException ex) {
Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
}
}
String[] list = file.list();
for (String string : list) {
System.out.println(string);
}
//Ung dung kha nhieu: Viet dc 1 chuong duyet toan file trong 1 folder
//Viet 1 ung dung: Explorer
}
}
#Test.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 aptech.java2.lession3;
/**
*
* @author Diep.Tran
*/
public class Test {
public static void main(String[] args) {
DataController<People> controller1 = new DataController<>();
controller1.add(new People("A1", "Ha Noi"));
controller1.add(new People("A2", "Ha Nam"));
// People p1 = new People("A3", "OKOK");
controller1.add(new People("A3", "Ha Nam"));
DataController<People> controller2 = new DataController<>();
controller2.add(new People("A4", "Ha Nam"));
controller1.display();
// DataController<String> controller = new DataController<>();
// controller.add("123");
// controller.add("234324234");
//
// controller.display();
DataController<Number> c2 = new DataController<>();
}
}
#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 aptech.java2.lession3;
/**
*
* @author Diep.Tran
*/
public class Student extends People{
String rollNo;
public Student() {
}
public Student(String rollNo, String name, String address) {
super(name, address);
this.rollNo = rollNo;
}
public String getRollNo() {
return rollNo;
}
public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}
@Override
public String toString() {
return super.toString() + "Student{" + "rollNo=" + rollNo + '}';
}
}
#People.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 aptech.java2.lession3;
/**
*
* @author Diep.Tran
*/
public class People {
String name, address;
public People() {
}
public People(String name, String address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "People{" + "name=" + name + ", address=" + address + '}';
}
public String getLine() {
return name + "," + address+"\n";
}
}
#Number.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 aptech.java2.lession3;
/**
*
* @author Diep.Tran
*/
public class Number{
int x1, x2;
}
#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 aptech.java2.lession3;
import java.util.ArrayList;
import java.util.HashMap;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
//Khai bao 1 mang quan ly danh sach String
ArrayList<String> arr1 = new ArrayList<>();
arr1.add("Vi du 1");
arr1.add("Vi du 2");
arr1.add("Vi du 3");
// arr1.add(12);
String s = arr1.get(1);
System.out.println("s: " + s);
//Khai bao 1 mang quan ly danh sach so nguyen
ArrayList<Integer> arr2 = new ArrayList<>();
arr2.add(1);
arr2.add(5);
// arr2.add("sdasdasd");
int t = arr2.get(0);
int[] k = new int[10];
System.out.println("t: " + t);
//Ket luan nhu sau
//1. ArrayList<String> & ArrayList<Integer> => Cung dang tro toi 1 class object
//2. add(String) & add(Integer) -> dang su dung 1 ham duy nhat => Ko phai la overloading
HashMap<String, Integer> map = new HashMap<>();
map.put("name", 12);
map.put("age", 12);
// DataController<String> controller = new DataController<>();
// controller.add("123");
// controller.add("234324234");
//
// controller.display();
}
}
#FileInOutStream.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 aptech.java2.lession3;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class FileInOutStream {
public static void main(String[] args) {
//Test read
// readFile();
//Test write
// writeFile();
writeFileStudent();
}
static void writeFileStudent() {
FileOutputStream fos = null;
try {
File file = new File("student.txt");
fos = new FileOutputStream(file);
People p1 = new People("A1", "Ha Noi");
byte[] b1 = p1.getLine().getBytes("utf8");
fos.write(b1);
People p2 = new People("A2", "Nam Dinh");
byte[] b2 = p2.getLine().getBytes("utf8");
fos.write(b2);
//Luu tru du lieu cua 2 object p1 & p2 => File => student.txt
//Moi sinh vien luu tren 1 line.
//Dinh nghia 1 cau truc du lieu: name, address
} catch (FileNotFoundException ex) {
Logger.getLogger(FileInOutStream.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(FileInOutStream.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FileInOutStream.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(FileInOutStream.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void writeFile() {
FileOutputStream fos = null;
try {
File file = new File("vidu.txt");
fos = new FileOutputStream(file, true);
String s = "\nSinh viên Aptech rweryu 348738234 23487234238 4";
byte[] b = s.getBytes("utf8");
fos.write(b);
} catch (FileNotFoundException ex) {
Logger.getLogger(FileInOutStream.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(FileInOutStream.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FileInOutStream.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(FileInOutStream.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void readFile() {
FileInputStream fis = null;
// InputStreamReader reader = null;
// BufferedReader bReader = null;
try {
File file = new File("vidu.txt");
fis = new FileInputStream(file);
// reader = new InputStreamReader(fis, StandardCharsets.UTF_8);
// bReader = new BufferedReader(reader);
int code;
// byte[] b = new byte[1024];
// int length = fis.read(b);
// String s = new String(b, 0, length);
// System.out.println(s);
while((code = fis.read()) != -1) {
//Convert code -> char
char c = (char) code;
System.out.print(c);
}
// String s;
// while((s = bReader.readLine()) != null) {
// System.out.println(s);
// }
} catch (FileNotFoundException ex) {
Logger.getLogger(FileInOutStream.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FileInOutStream.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if(fis != null) {
fis.close();
}
} catch (IOException ex) {
Logger.getLogger(FileInOutStream.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
#DataController.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 aptech.java2.lession3;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Diep.Tran
*/
public class DataController<E> {
List<E> dataList;
public DataController() {
dataList = new ArrayList<>();
}
public void add(E e) {
dataList.add(e);
}
public void display() {
for (E e : dataList) {
System.out.println(e);
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)