By GokiSoft.com|
20:14 20/07/2022|
Java Advanced
[Source Code] Tìm hiểu Generic & File trong Java - C2108L
#C2108L.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 bt1058;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class C2108L {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
HashMap<String, Student> studentList = new HashMap<>();
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so sinh vien can them: ");
int n = Integer.parseInt(scan.nextLine());
for (int i = 0; i < n; i++) {
Student std = new Student();
std.input();
//Them phan tu nay vao day???
//Su dung rollno lam key trong mang HashMap
studentList.put(std.getRollno(), std);
}
//Duyet qua cac phan tu trong HashMap
for (Map.Entry<String, Student> entry : studentList.entrySet()) {
String key = entry.getKey();
Student std = entry.getValue();
System.out.println("KEY: " + key);
std.display();
}
System.out.println("Nhap MSV can tim kiem: ");
String rollno = scan.nextLine();
Student std = studentList.get(rollno);
if(std != null) {
std.display();
} else {
System.out.println("Ko tim thay sinh vien co msv: " + rollno);
}
System.out.println("Nhap MSV can tim kiem: ");
rollno = scan.nextLine();
std = studentList.get(rollno);
if(std != null) {
std.display();
} else {
System.out.println("Ko tim thay sinh vien co msv: " + rollno);
}
}
}
#DataMgr.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 bt1058;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Administrator
*/
public class DataMgr<E extends Student>{
List<E> dataList;
public DataMgr() {
dataList = new ArrayList<>();
}
public void add(E e) {
dataList.add(e);
}
public void input() {
System.out.println("Nhap du lieu");
}
public void display() {
for (E e : dataList) {
System.out.println(e);
}
}
}
#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 bt1058;
import java.util.Scanner;
/**
*
* @author Administrator
*/
public class Student {
String rollno, name, gender, email, address;
int age;
public Student() {
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap MSV: ");
rollno = scan.nextLine();
System.out.println("Nhap ten: ");
name = scan.nextLine();
System.out.println("Nhap gioi tinh: ");
gender = scan.nextLine();
System.out.println("Nhap email: ");
email = scan.nextLine();
System.out.println("Nhap dia chi: ");
address = scan.nextLine();
System.out.println("Nhap tuoi: ");
age = Integer.parseInt(scan.nextLine());
}
public void display() {
System.out.println(this);
}
@Override
public String toString() {
return "rollno=" + rollno + ", name=" + name + ", gender=" + gender + ", email=" + email + ", address=" + address + ", age=" + age + "\n";
}
public String getRollno() {
return rollno;
}
public void setRollno(String rollno) {
this.rollno = rollno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
#Test01.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 bt1058;
import java.util.ArrayList;
/**
*
* @author Administrator
*/
public class Test01 {
public static void main(String[] args) {
//Noi dung kien thuc
//Generic:
// Class Object
// Khoi tao object -> Chuyen kieu du lieu quan vao trong luc khoi tao object
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(12);
list1.add(3);
list1.add(102);
for (Integer v : list1) {
System.out.println("v = " + v);
}
//Vi du 2
ArrayList<String> list2 = new ArrayList<>();
list2.add("Vi du 1");
list2.add("Vi du 2");
list2.add("Vi du 3");
for (String v : list2) {
System.out.println("v = " + v);
}
DataMgr<Student> d1 = new DataMgr<>();
d1.add(new Student());
// d1.add("123123");
d1.input();
d1.display();
// DataMgr<String> d2 = new DataMgr<>();
// d2.add("asds");
// d2.add("tertert");
// d2.add("ertertret");
}
}
#Test2.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 bt1058;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
public class Test2 {
public static void main(String[] args) {
//testFile();
//Ghi noi dung du lieu vao File
// testWriteFile();
// testWriteFile2();
// testWriteFile3();
// testReadFile();
// testReadFile2();
testWriteFile4();
}
static void testWriteFile4() {
String filename = "vidu3.txt";
FileWriter writer = null;
BufferedWriter bWriter = null;
try {
writer = new FileWriter(filename);
bWriter = new BufferedWriter(writer);
String line = "Sinh viên Aptech 54 Lê Thanh Nghi";
bWriter.write(line);
} catch (IOException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(bWriter != null) {
try {
bWriter.close();
} catch (IOException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(writer != null) {
try {
writer.close();
} catch (IOException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void testReadFile2() {
String filename = "student.txt";
//Thu vien de su dung -> do nghi noi dung utf8
FileReader reader = null;
BufferedReader bReader = null;
try {
reader = new FileReader(filename);
bReader = new BufferedReader(reader);
String line;
while((line = bReader.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(reader != null) {
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(bReader != null) {
try {
bReader.close();
} catch (IOException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void testReadFile() {
String filename = "student.txt";
//Khai bao
FileInputStream fis = null;
StringBuilder builder = new StringBuilder();
try {
//B1. Mo ket noi file
fis = new FileInputStream(filename);
//B2. Doc noi dung
int code;
while((code = fis.read()) != -1) {
builder.append((char)code);
}
System.out.println(builder.toString());
} catch (FileNotFoundException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//B3. Dong ket noi
if(fis != null) {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
private static void testWriteFile3() {
String filename = "student.txt";
Scanner scan = new Scanner(System.in);
//Nhap thong tin N sinh vien
System.out.println("Nhap N sinh vien: ");
int N = Integer.parseInt(scan.nextLine());
ArrayList<Student> stdList = new ArrayList<>();
for (int i = 0; i < N; i++) {
Student std = new Student();
std.input();
stdList.add(std);
}
//Luu thong tin sinh vien vao file student.txt -> thong tin sinh vien -> moi thang 1 dong
//Khai bao thu vien se su dung trong luu file
FileOutputStream fos = null;
//B1. Mo ket noi toi File
try {
fos = new FileOutputStream(filename);
//B2. Ghi noi dung vao File
for (Student student : stdList) {
String line = student.toString();
byte[] bytes = line.getBytes("utf8");
fos.write(bytes);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//B3. Dong ket noi
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void testWriteFile() {
String filename = "vidu1.txt";
// String filename = "C:\\Users\\Administrator\\Desktop\\vidu.txt";
//Khai bao thu vien se su dung trong luu file
FileOutputStream fos = null;
//B1. Mo ket noi toi File
try {
fos = new FileOutputStream(filename);
//B2. Ghi noi dung vao File
String line = "Sinh vien Aptech 54 Le Thanh Nghi";
byte[] bytes = line.getBytes("utf8");
fos.write(bytes);
} catch (FileNotFoundException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//B3. Dong ket noi
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
private static void testWriteFile2() {
String filename = "vidu2.txt";
Scanner scan = new Scanner(System.in);
//Khai bao thu vien se su dung trong luu file
FileOutputStream fos = null;
//B1. Mo ket noi toi File
try {
fos = new FileOutputStream(filename, true);
//B2. Ghi noi dung vao File
String line;
do {
line = scan.nextLine();
byte[] bytes = (line + "\n").getBytes("utf8");
fos.write(bytes);
} while(!line.equalsIgnoreCase("N"));
} catch (FileNotFoundException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//B3. Dong ket noi
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Test2.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
/**
* Phan 1: Test File
*/
static void testFile() {
String filePath = "C:\\Users\\Administrator\\Downloads\\Professional Programming in Java - Slide.zip";
File file = new File(filePath);
if(file.exists()) {
System.out.println("Duong dan ton tai");
} else {
System.out.println("Duong dan ko ton tai");
}
if(file.isFile()) {
System.out.println("Duong dan la File");
} else {
System.out.println("Duong dan la folder");
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)