0. byte stream and binary files
My code
public class WriterStu {
public static void main(String[] args) {
DataOutputStream dos = null;
Student[] stus = new Student[100];
Student d1 = new Student(1, "x", 18, 99.5);
Student d2 = new Student(2, "x", 19, 100.0);
Student d3 = new Student(3, "x", 20, 59.5);
try (FileOutputStream fos = new FileOutputStream(new File("e:/Student.data"))) {
dos = new DataOutputStream(fos);
for (Student student : stus) {
dos.writeInt(student.getId());
dos.writeUTF(student.getName());
dos.writeInt(student.getAge());
dos.writeDouble(student.getGrade());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try (DataInputStream dis = new DataInputStream(new FileInputStream("e:/Student.data"))) {
int id = dis.readInt();
String name = dis.readUTF();
int age = dis.readInt();
double grade = dis.readDouble();
Student stu=new Student(id,name,age,grade);
System.out.println(stu);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
My summary
Data in the text file are organized in the form of characters, typically to be read line by line or all of a string variable. Binary data types may be substantially stored int / double / char like.
1. stream of characters and text files: Use the PrintWriter (write), BufferedReader (read)
My code
4.
String fileName1="e:/data.txt";
try(
FileOutputStream fos=new FileOutputStream(fileName1);
ObjectOutputStream oos=new ObjectOutputStream(fos))
{
Student s=new Student(5,"l",12,85);
oos.writeObject(s);
}
catch (Exception e) {
e.printStackTrace();
}
try(
FileInputStream fis=new FileInputStream(fileName1);
ObjectInputStream ois=new ObjectInputStream(fis))
{
Student newStudent =(Student)ois.readObject();
System.out.println(newStudent);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
My summary
The object flow written in txt garbled happens, the gibberish instead dat format does not happen.
2. The stream buffer (binding test using JUint)
My code
public class PrintWriterTest {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("e:/data.txt");
if(file.exists())
{
System.out.println("文件已存在!");
System.exit(0);
}
PrintWriter pw=new PrintWriter(file);
Random r=new Random(100);
for(int i=0;i<1000_0000;i++) {
int j=r.nextInt(11);
pw.println(j);
}
pw.close();
}
}
JunitTest
public class JunitTest {
@Test
public void testBufferedReader() {
BufferedReader br = null;
int n = 0, sum = 0;
double average = 0;
try {
br = new BufferedReader(new FileReader("e:/data.txt"));
String line = null;
try {
while ((line = br.readLine()) != null) {
int num = Integer.parseInt(line);
n++;
sum += num;
}
average = sum / n;
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
System.out.format("%d %d %.5f", n, sum, average);
System.out.println();
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testScanner() {
File file = new File("e:/data.txt");
Scanner sc = null;
int n = 0, sum = 0;
double average = 0;
try {
sc = new Scanner(file);
while (sc.hasNext()) {
int num = sc.nextInt();
sum+=num;
n++;
}
average = sum / n;
System.out.format("%d %d %.5f", n, sum, average);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
My summary
Use BufferedReader faster, BufferedReader buffer can reduce the use of IO times, since the IO time-consuming operation, so that the reading speed is increased.
3. Object of the byte stream flow
My code
public static void writeStudent(List<Student> stuList) {
String fileName = "e:/data.txt";
try (FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream ois = new ObjectOutputStream(fos)) {
ois.writeObject(stuList);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
public static List<Student> readStudents(String fileName) {
List<Student> stuList = new ArrayList<>();
try (FileInputStream fis = new FileInputStream(fileName); ObjectInputStream ois = new ObjectInputStream(fis)) {
stuList = (List<Student>) ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return stuList;
}
My summary
ObjectInputStream ObjectOutputStream and when used in conjunction with each FileOutputStream and FileInputStream, may provide persistent storage for the object graphics applications.
5. File Operations
My code
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Files {
Path dir=Paths.get("e:/data.txt");
public static void findFile(Path dir,String fileName) {
File file =dir.toFile();
File[] files =file.listFiles();
for(File file1:files) {
if(file1.isDirectory()) {
findFile(file1.toPath(),fileName);
}else {
if(file1.equals(new File(fileName))) {
System.out.println("Found:");
System.out.println(file1.getAbsolutePath());
}
}
}
}
}
My summary
Path File class and each class can be converted, Paths Path object class can be obtained directly, does not require new Path.
6. Regular Expressions
Determining whether a given string is a decimal number format
My code
import java.util.regex.Pattern;
public class testIsDigit {
public static void main(String[] args) {
System.out.println(isDigit("123a"));
System.out.println(isDigit("-123"));
System.out.println(isDigit("a123a"));
System.out.println(isDigit("123"));
System.out.println(isDigit("-"));
}
public static boolean isDigit(String s) {
return Pattern.matches("^-?\\d+$", s);
}
}
My summary
Familiar with regular expression syntax can help us get easier to write code.