十二、字符流
字符流顶级父类,Reader和Writer Reader是字符输入流从某个源设备读取字符 Writer是字符输出流向某个目标写入字符。
1、FileRead
import java.io.FileReader;
public class FileRead {
public static void main(String[] args) throws Exception{
FileReader fileReader = new FileReader("C:/Users/20665/OneDrive/桌面/java/io/read.txt");
int len;
while((len=fileReader.read())!=-1){
System.out.print((char) len);
}
fileReader.close();
}
}
2、字符流Buffer缓冲流对象
把read.txt文件里面的数据读取,然后写入到read2.txt文件中
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class Buffer {
public static void main(String[] args) throws Exception{
FileReader reader = new FileReader("C:/Users/20665/OneDrive/桌面/java/io/read.txt");
BufferedReader br = new BufferedReader(reader);
FileWriter fileWriter = new FileWriter("C:/Users/20665/OneDrive/桌面/java/io/read2.txt");
BufferedWriter bw = new BufferedWriter(fileWriter);
String str;
while((str = br.readLine())!=null){
bw.write(str+"\n");
bw.newLine();
}
br.close();
bw.close();
}
}
3、字节流转字符流
import java.io.*;
public class Change {
public static void main(String[] args) throws Exception{
//创建字节输入流
FileInputStream fileInputStream = new FileInputStream("C:/Users/20665/OneDrive/桌面/java/io/read.txt");
//字节输入流转化为字符输入流
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
//创建字符流对象缓冲区
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//创建字节输出流
FileOutputStream fileOutputStream = new FileOutputStream("C:/Users/20665/OneDrive/桌面/java/io/read2.txt");
//将字节输出流转化为字符输出流
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
//创建字符流对象缓冲区
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
String str;
while((str= bufferedReader.readLine())!=null){
bufferedWriter.write((str)+"\n");
bufferedWriter.newLine();
}
bufferedWriter.close();
bufferedReader.close();
}
}
十三、文件操作
1、基本文件操作
import java.io.File;
public class WenJian {
public static void main(String[] args) {
File file = new File("C:/Users/20665/OneDrive/桌面/java/io/read.txt");
//得到文件名
System.out.println(file.getName());
//绝对路径
System.out.println(file.getAbsoluteFile());
//相对路径
System.out.println(file.getPath());
//判断文件是否可读
System.out.println(file.canRead() ? "yes" : "no");
//判断是否是一个文件
System.out.println(file.isFile());
}
}
2、DeleteFile
import java.io.File;
public class DeleteFile {
public static void main(String[] args) {
File file = new File("C:/Users/20665/OneDrive/桌面/java/io");
if(file.exists()){
//判断文件是否为空,为空则删除文件夹
System.out.println(file.delete());
}
}
}
3、遍历文件夹内容
import java.io.File;
public class WenJianJia {
public static void main(String[] args) {
File file = new File("C:/Users/20665/OneDrive/桌面/java/io");
//判断是否为文件夹
if (file.isDirectory()){
String[] read = file.list();
for (String string : read) {
System.out.println(string);
}
}
}
}
十四、单例设计模式
单例模式主要是为了避免因为创建了多个实例造成资源的浪费,且多个实例由于多次调用容易导致结果出现错误,而使用单例模式能够保证整个应用中有且只有一个实例。从其名字中我们就可以看出所谓单例,就是单个实例也就是说它可以解决的问题是:可以保证一个类在内存中的对象的唯一性,在一些常用的工具类、线程池、缓存,数据库,账户登录系统、配置文件等程序中可能只允许我们创建一个对象,一方面如果创建多个对象可能引起程序的错误,另一方面创建多个对象也造成资源的浪费。
Single类
package com.jn.model;
public class Single {
private static Single INSTANCE = new Single();
private Single() {
}
public static Single getInstance() {
return INSTANCE;
}
}
class Single1 {
private static Single1 INSTANCE = null;
private Single1() {
}
public static Single1 getInstance() {
if (INSTANCE == null) {
INSTANCE = new Single1();
}
return INSTANCE;
}
}
测试类
public class SignalTest {
public static void main(String[] args) {
Single s = Single.getInstance();
Single s2 = Single.getInstance();
System.out.println(s == s2);
Single1 s1 = Single1.getInstance();
Single1 s3 = Single1.getInstance();
System.out.println(s1 == s3);
}
}
十五、连接mysql数据库
package com.jn.liumaishengjian;
import java.sql.*;
public class JDBCDemo {
public static void main(String[] args) {
String ur1 = "jdbc:mysql://localhost:3306/shixun?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
String user = "root";
String password = "123456";
try{
//加载程序驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//建立连接
Connection conn = DriverManager.getConnection(ur1, user, password);
//创建Statement对象
Statement stmt = conn.createStatement();
//执行查询
ResultSet rs = stmt.executeQuery("select * from stu");
int idWidth = 6; // 根据实际情况调整,假设 ID 最长为 4 个字符
int nameWidth = 9; // 假设名字最长为 8 个字符
int ageWidth = 3; // 假设年龄最长为 3 个字符
int genderWidth = 6; // 假设性别最长为 6 个字符
// 输出表头
System.out.printf("%-" + idWidth + "s %-" + nameWidth + "s %-" + ageWidth + "s %-" + genderWidth + "s%n",
"ID", "Name", "Age", "Gender");
while (rs.next()) {
String id = rs.getString("sid");
String name = rs.getString("sname");
int age = rs.getInt("age");
String gender = rs.getString("gender");
System.out.printf("%-" + idWidth + "s %-" + nameWidth + "s %-" + ageWidth + "s %-" + genderWidth + "s%n",
id, name, age, gender == null? "" : gender);
}
//关闭连接
rs.close();
stmt.close();
conn.close();
}catch (Exception e){
e.printStackTrace();
}
}
}