Text
Binary
File -> Rất nhiều thư viên được sử dung thao tác vs File
-> Chính được sử dụng trong dự án & thi cử
File
FileInputStream
FileOutputStream
BufferedReader
BufferedWriter
BufferedInputStream
BufferedOutputStream
ObjectInputStream
ObjectOutputStream
=======================================================================
Path:
- Đường dẫn tương đối
- Đường dẫn tuyệt đối
/*
* 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 com.gokisoft.bt1058;
import java.util.ArrayList;
/**
*
* @author teacher
*/
public class Test01 {
public static void main(String[] args) {
//Phan tich khai bao bien
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(12);
list1.add(33);
// list1.add("12312312");
int t = list1.get(0);
System.out.println(t);
ArrayList<String> list2 = new ArrayList<>();
list2.add("123123");
list2.add("34sdfsdf");
// list2.add(123);
String s = list2.get(0);
System.out.println(s);
// TestController<String> controller = new TestController<>();
// controller.add("Vi du 1");
// controller.add("Vi du 2");
// controller.add("Vi du 3");
//
// controller.display();
TestController<Student> controller2 = new TestController<>();
controller2.add(new Student());
controller2.add(new Student());
controller2.display();
}
}
/*
* 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 com.gokisoft.bt1058;
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.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
public class Test02 {
public static void main(String[] args) {
//test01();
//test02();
//test03();
//test04();
test06();
}
static void test06() {
String filename = "students.txt";
FileInputStream fis = null;
try {
//B1. Mo file
fis = new FileInputStream(filename);
StringBuilder builder = new StringBuilder();
int code;
while((code = fis.read()) != -1) {
if((char) code == "\n".charAt(0)) {
//1 dong chua thong tin sinh vien
String line = builder.toString();
Student std = new Student();
std.parse(line);
std.display();
builder = new StringBuilder();
} else {
builder.append((char) code);
}
}
// System.out.println(builder.toString());
} catch (FileNotFoundException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//B3. Dong file
if(fis != null) {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void test05() {
String filename = "students.txt";
FileInputStream fis = null;
try {
//B1. Mo file
fis = new FileInputStream(filename);
StringBuilder builder = new StringBuilder();
int code;
while((code = fis.read()) != -1) {
builder.append((char) code);
// System.out.println((char) code);
}
System.out.println(builder.toString());
} catch (FileNotFoundException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//B3. Dong file
if(fis != null) {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void test04() {
Student std1 = new Student();
Student std2 = new Student();
std1.input();
std2.input();
// std1.display();
// std2.display();
String filename = "students.txt";
FileOutputStream fos = null;
try {
//B1. Mo file
fos = new FileOutputStream(filename, true);
//B2. Doc & Ghi du lieu
byte[] data = std1.toString().getBytes("utf8");
fos.write(data);
data = std2.toString().getBytes("utf8");
fos.write(data);
} catch (FileNotFoundException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//B3. Dong file
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void test03() {
String filename = "vidu.txt";
Scanner scan = new Scanner(System.in);
FileOutputStream fos = null;
try {
//B1. Mo file
fos = new FileOutputStream(filename, true);
//B2. Doc & Ghi du lieu
String str;
do {
System.out.println("Nhap text can them vao file: ");
str = scan.nextLine();
byte[] data = ("\n" + str).getBytes("utf8");
fos.write(data);
} while(!str.equalsIgnoreCase("N"));
} catch (FileNotFoundException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//B3. Dong file
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void test02() {
String filename = "vidu.txt";
String str = "\nSinh viên Aptech 54 Lê Thanh Nghị";
FileOutputStream fos = null;
try {
//B1. Mo file
fos = new FileOutputStream(filename, true);
//B2. Doc & Ghi du lieu
byte[] data = str.getBytes("utf8");
fos.write(data);
} catch (FileNotFoundException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//B3. Dong file
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
static void test01() {
String filePath = "C:\\Users\\teacher\\Downloads\\Professional Programming in Java - Slide\\1.Slide\\Session 6.pdf";
File file = new File(filePath);
if(file.isFile()) {
System.out.println("File > " + file.canRead() + ", " + file.canWrite() + ", " + file.canExecute());
} else {
System.out.println("Folder");
}
}
}
/*
* 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 com.gokisoft.bt1058;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author teacher
*/
public class TestController<E extends Student> {
List<E> dataList = new ArrayList<>();
public void add(E e) {
dataList.add(e);
}
public void display() {
for (int i = 0; i < dataList.size(); i++) {
System.out.println("Test ...");
}
}
}
Ứng Dụng Học
Theo dõi cập nhật nội dung học trên Youtube & Facebook
Thông Tin Liên Hệ
Công Ty Cổ Phần Công nghệ ZicZac Việt Nam.
Website: https://gozic.vn
SĐT: 096 - 70 25 996
Email: ziczacgroup@gmail.com
Thiết kế webiste chuyên nghiệp
Thiết kế phần mềm quản trị
Thiết kế ứng dụng Android
Thiết kế ứng dụng IOS
Thiết kế Web App
Hỗ trợ Digital Marketing
Hỗ trợ quảng cáo Google Ads
Hỗ trợ quảng cáo Facebook Ads
Hỗ trợ SEO Website