个人曾经学习java的一些记录,仅供参考
java学习-day06
jar包
jar包就是java的字节码(.class)的压缩包。 JDK其实就是提供了很多这样的jar包,jar包中提供了很多开发的工具类。我们如果需要去操作数据库,那么需要提供对应数据库的jar包
数据库连接
Oracle
配置Oracle支持远程连接:
配置listener.ora
配置tnsnames.ora
登录数据库
关闭数据库
shutdown
重启数据库
确认远程连接
配置远程客户端
下载sqlplus
将tnsnames.ora拷贝到下载的sqlplus中
配置环境变量
tnsnames.ora文件所在的路径
字符集
NLS_LANG
SIMPLIFIED CHINESE_CHINA.ZHS16GBK
安装PL/SQL
java操作oracle
package com.dst.db01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Test {
public static void main(String[] args) {
/*
PS : 写自己的IP,自己的数据库实例名称
jdbc:oracle:thin:192.168.10.10:1521:orcl
jdbc:oracle:thin: =>协议
192.168.10.10 => IP地址,客户机必须和Oracle所在的主机可以进行网络访问
1521 => 端口号 Oracle的 service会监听1521这个端口号,当有人访问这个端口的时候就知道是访问的Oracle数据库
orcl => 数据库的实例名称
*/
Connection conn = null;
try {
//加载驱动: 驱动类都是实现了Driver接口。都是Driver类的子类。Driver就是JDK对外提供的统一接口
//如果我们需要去访问某个数据库,则先需要加载对应的驱动类,而这个驱动类由对应的数据库厂商提供
//加载驱动类的时候,这个驱动类可能不存在,也可能会把这个路径写错
Class.forName("oracle.jdbc.driver.OracleDriver");//参数为驱动所在的路径
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.10.10:1521:orcl",
"hr",
"123456"
);
System.out.println(conn);//如果成功输出则表示数据库连接成功
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException e){
e.printStackTrace();
}finally {
try {
conn.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
}
文件系统类File
学一个新的API的要点:
1、对象如何创建(看这个类是普通类还是抽象类还是接口)
2、这个工具类给我们提供了哪些API
3、可以做什么
工具类的地址https://hutool.cn/docs/
文件类只会操作文件系统(属性结构),提供了对文件路径的API,而对于文件内容没有API。文件内容需要通过IO来进行操作
案例1:创建和删除文件
删除文件
案例2:递归获取文件包含子文件的文件名
2、IO
2.1 文件流 (字节流、字符流-文本文件)
2.1.1 文件字符流
读取文件
public void readFile(String filePath) {
File file = new File(filePath);
FileReader reader = null;
try {
reader = new FileReader(file);
//reader.read();//每次读取一个字符,这个太慢了
//定义一个缓冲区,用来保存每次读取的字符数
char[] buf = new char[10];//缓冲区每次只能保存最大10个字符
//当reader方法返回值是 -1 的时候表示文件读取完毕
int len = 0; //接受每次读取的字符数 长度
while ((len = reader.read(buf)) != -1) {
//将字符转成字符数组,你都多少就转多少 len
System.out.println(new String(buf, 0, len));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// IO一定要关闭
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
写文件
public void writeFile(String filePath) {
FileWriter writer = null;
try {
writer = new FileWriter(filePath);
// 编码的问题
writer.write(new String("你好 ".getBytes(Charset.forName("GBK")),Charset.forName("GBK")));
writer.write("html\n");
writer.write("hello css");
writer.flush();//刷新
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.1.2 文件流字符流
拷贝文件
//字节流拷贝文件
public void copy(String src,String dst){
File srcFile = new File(src);
File dstFile = new File(dst);
FileInputStream in = null; // 字节流 读
FileOutputStream out = null;// 字节流 写
//判断源文件是否存在
if(!srcFile.exists()){
// 异常中断
throw new RuntimeException(src+"不存在");
}
//判断目标路径是否存在
File parentFile = dstFile.getParentFile();
if(!parentFile.exists()) parentFile.mkdirs();
//开始拷贝文件
try {
in = new FileInputStream(srcFile);
out = new FileOutputStream(dstFile);
// 1、读取文件: 读取到一个缓冲区
byte[] buf = new byte[1024];
int len = 0;
while ((len=in.read(buf))!=-1){
// 2、写入文件: 将缓冲区的内容写入到指定文件
out.write(buf,0,len);
out.flush();
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
in.close(); //先关读
out.close(); //再关写
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.2 字节数组流
字节数组流是一个中间流,可以理解为是一个缓冲流
向字节数组流中保存数据
// 将内容读取后保存到一个字节数组流中:字节数组流是一个中间层,不需要关闭
public void byteArrayRead(){
//将需要保存的内容转成一个字节数组
String word = "hello world";
byte[] bytes = word.getBytes();
//将字节数组装配到字节数组流中
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
// 从字节数组中读出
byte[] buf = new byte[10];
int len = 0;
try {
while ((len = in.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
}catch (Exception e){
e.printStackTrace();
}
}
public void byteArrayWrite(){
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
out.write("hello world".getBytes());
System.out.println(out.toString());
System.out.println(out.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
}
案例:爬虫???
// 爬虫
public void crawl(String path){
try {
//获取统一资源定位对象
URL url = new URL(path);
//连接网络
URLConnection conn = url.openConnection();
//获取网络流
InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));//装饰模式
while (reader.ready()){
System.out.println(reader.readLine());
}
} catch (Exception e) {
e.printStackTrace();
}
}
2.3 对象流
//保存对象到文件
public void saveUser(){
// User 必须是Serializable子类
// 序列化:保存了这个类的属性、属性的类型、方法、继承关系等....
User user = new User();
user.setName("jack");
user.setAge(20);
String word = "hello";
ObjectOutputStream out = null;
FileOutputStream fo = null;
try {
fo = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\day12-2\\user.dat");
out = new ObjectOutputStream(fo);
out.writeUTF(word);
out.writeObject(user);
out.flush();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
out.close();
fo.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void readUser(){
FileInputStream in = null;
ObjectInputStream oi = null;
String userDat = "C:\\Users\\Administrator\\Desktop\\day12-2\\user.dat";
try {
in = new FileInputStream(userDat);
oi = new ObjectInputStream(in);
//读的顺序和写的顺序要一致
//先读 : word
String word = oi.readUTF();
//再 user
User user = (User) oi.readObject();
System.out.println(word);
System.out.println(user);
}catch (Exception e){
e.printStackTrace();
}finally {
}
}
buffered
public class BufIO {
public void readFile() {
FileReader fr = null;
BufferedReader reader = null;
try {
fr = new FileReader("C:\\Users\\Administrator\\Desktop\\day13\\word.txt");
reader = new BufferedReader(fr);
while (reader.ready()) {
System.out.println(reader.readLine());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
reader.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void writeFile() {
FileWriter fw = null;
BufferedWriter writer = null;
try {
fw = new FileWriter("C:\\Users\\Administrator\\Desktop\\day13\\word.txt");
writer = new BufferedWriter(fw);
writer.write("hello world");
writer.newLine();// 换行
writer.write("hello java");
writer.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
writer.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
总结
什么是流: 流就是数据流动,数据的流向。我们可以将流比喻成是流水
流源:数据从哪里来?
流汇:数据到哪里去?
分类:
按流向:输入流和输出流
按类型:字节流(InputStream OutputStrram的子类)和字符流(Reader Writer的子类)
按功能:节点流和处理流(流的构造参数是另外一个流)
流的架构
InputStream
FileInputStream
ByteArrayInputStream
ObjectInpuStream
OutpuStream
…
Reader
FileWriter
BufferedWriter
Writer
…