ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

JAVA之IO流,mysql主从复制原理简书

2021-11-13 14:06:53  阅读:179  来源: 互联网

标签:主从复制 简书 String len static new close JAVA public


在这里插入图片描述


流的分类

===================================================================

在这里插入图片描述


文件字节输入流FileInputStream ----将文件中的数据读取出来

=====================================================================================================

FileInputStream in=new FileInputStream(“D:\IO\dhy.txt”);

//设置一个byte数组接收读取的文件的内容

byte[] b=new byte[10];

//设置一个读取数据的长度

int len=0;

//read方法有一个返回值,返回值是读取的数据的长度,如果读取到最后一个数据,还会向后读取一个,这时候返回值就是-1

//所以当read的返回值是-1时,整个文件就读取完毕了

// in.read(b);

//不断从文件中读取数据,返回读取长度,直到等于-1时,说明读取结束

while((len=in.read(b))!=-1)

{

//第一个参数: 缓冲数据的数组 第二个参数:从数组的哪个位置开始转化字符串 第三个参数:总共转化几个字节

System.out.println(new String(b,0,len));

}

in.close();//流在使用完毕之后一定要关闭

在这里插入图片描述


文件字节输出流FileOutputStream—将数据写入文件中

===============================================================================================

//指定向dhy.txt里面输出数据

//如果当前文件夹下面没有对应的.txt,那么会帮我们创建出来一个

FileOutputStream out=new FileOutputStream(“D:\IO\xpy.txt”);

String str=“大忽悠”;

out.write(str.getBytes(StandardCharsets.UTF_8));//把数据写到内存中,这里是以字节方式写入的

out.flush();//把内存中的数据刷写到硬盘上

out.close();//关闭流

在这里插入图片描述


文件字节输入输出流复制图片案例

==============================================================================

import java.io.*;

import java.nio.charset.StandardCharsets;

public class test {

public static void main(String[] args) {

CopyData(“D:\image\long.jpg”,“D:\IO\dhy.jpg”);

}

//inPath: 从哪个文件中读取数据 outPath:将读取出来的对应的数据写入到哪个文件中

public static void CopyData(String inPath,String outPath)

{

try {

FileInputStream in=new FileInputStream(inPath);

FileOutputStream out=new FileOutputStream(outPath);

byte[] bytes=new byte[1024];

int len=0;

while((len=in.read(bytes))!=-1)

{

//读取到内存中

out.write(bytes,0,len);//参数一是缓冲数组,参数2是从数组中的哪个位置开始读取,参数3是读取的长度

}

//将读取数据刷新到硬盘上

out.flush();

//安装逆序的顺序关闭流

out.close();

in.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

在这里插入图片描述


文件字节流非常通用,可以用来操作字符的文档,还可以用来操作其他任何文件(图片,压缩包等),因为字节流使用的是二进制



文件字符输入流FileReader----将文件中的数据读取出来

===============================================================================================

在这里插入图片描述

public class test {

public static void main(String[] args) {

CopyData(“D:\IO\dhy.txt”);

}

public static void CopyData(String inPath)

{

try {

//创建文件字符输入流对象

FileReader fr=new FileReader(inPath);

//创建临时存数据的字符数组

char[] ch=new char[10];

//定义一个输入流的读取长度

int len=0;

while((len=fr.read(ch))!=-1)

{

System.out.println(new String(ch,0,len));

}

fr.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

在这里插入图片描述


文件字符输出流FileWriter----向文件中写入字符数据

==============================================================================================

import java.io.*;

import java.nio.charset.StandardCharsets;

public class test {

public static void main(String[] args) {

CopyData(“D:\IO\dpy.txt”,“大忽悠和小朋友”);

}

//outPath:将读取出来的对应的数据写入到哪个文件中 text:输出的内容

public static void CopyData(String outpath,String text)

{

try {

FileWriter fw=new FileWriter(outpath);

fw.write(text);//写到内存中

fw.flush();//把内存的数据刷到硬盘

fw.close();//关闭流

} catch (IOException e) {

e.printStackTrace();

}

}

}

在这里插入图片描述


字符流完成拷贝文件—只能拷贝文本文档

=================================================================================

import java.io.*;

import java.nio.charset.StandardCharsets;

public class test {

public static void main(String[] args) {

CopyData(“D:\IO\dpy.txt”,“D:\IO\copy\copy.txt”);

}

//outPath:将读取出来的对应的数据写入到哪个文件中 text:输出的内容

public static void CopyData(String inpath,String outpath)

{

try {

FileReader fr=new FileReader(inpath);

FileWriter fw=new FileWriter(outpath);

char[] c=new char[10];

int len=0;

//边读边写

while((len=fr.read©)!=-1)

{

fw.write(c,0,len);//写到内存中

}

fw.flush();//把内存的数据刷到硬盘

fw.close();//关闭流

fr.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

在这里插入图片描述


字节流和字符流共同需要注意的问题


在这里插入图片描述


处理流之一缓冲流

=======================================================================

在这里插入图片描述

字节缓冲输入流–BufferedInputStream


public class test {

public static void main(String[] args) {

try {

CopyData();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void CopyData() throws IOException {

//文件字节输入流对象

FileInputStream in=new FileInputStream(“D:\IO\dpy.txt”);

//把文件字节输入流放到缓冲字节输入流对象中

BufferedInputStream br=new BufferedInputStream(in);

byte[] b=new byte[100];

int len=0;

while((len=br.read(b))!=-1)

{

System.out.println(new String(b,0,len));

}

//关闭流的时候,按照最晚开的最关

br.close();

in.close();

}

}

在这里插入图片描述


字节缓冲输出流–BufferedOutputStream


public class test {

public static void main(String[] args) {

try {

CopyData();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void CopyData() throws IOException {

//创建字节输出流对象

FileOutputStream out=new FileOutputStream(“D:\IO\dpy.txt”);

//把字节输出流对象放到缓冲字节输出流中

BufferedOutputStream bo=new BufferedOutputStream(out);

String s=“hello world”;

//写到内存上

bo.write(s.getBytes(StandardCharsets.UTF_8));

//刷到硬盘上

bo.flush();

//关闭流的时候,最晚开的最早关闭,依次关

bo.close();

out.close();

}

}

在这里插入图片描述


缓冲流实现文件的复制


import java.io.*;

import java.nio.charset.StandardCharsets;

public class test {

public static void main(String[] args) {

try {

CopyData(“D:\image\long.jpg”,“D:\IO\dhy.jpg”);

} catch (IOException e) {

e.printStackTrace();

}

}

public static void CopyData(String inpath,String outpath) throws IOException {

//字节缓冲输入流

BufferedInputStream bi=new BufferedInputStream(new FileInputStream(inpath));

//字节缓冲输出流

BufferedOutputStream bo=new BufferedOutputStream(new FileOutputStream(outpath));

//用于存储数据的字节缓冲数组

byte[] b=new byte[1024];

//设置一个读取到的数组长度,直到读取到最后一个数据后面,返回-1

int len=0;

while((len=bi.read(b))!=-1)

{

bo.write(b,0,len);//写到内存上

}

bo.flush();//刷到硬盘上

bo.close();

bi.close();

}

}

在这里插入图片描述


字符缓冲输入流–BufferedReader

=====================================================================================

public class test {

public static void main(String[] args) {

try {

CopyData(“D:\IO\dpy.txt”);

} catch (IOException e) {

e.printStackTrace();

}

}

public static void CopyData(String inpath) throws IOException {

FileReader fileReader=new FileReader(inpath);

BufferedReader br=new BufferedReader(fileReader);

char[] c=new char[100];

int len=0;

//读到文件的最后一个字符的下一位,返回-1

while((len=br.read©)!=-1)

{

System.out.println(new String(c,0,len));

}

br.close();

fileReader.close();

}

}

在这里插入图片描述


字符缓冲输出流–BufferedWriter

=====================================================================================

import java.io.*;

import java.nio.charset.StandardCharsets;

public class test {

public static void main(String[] args) {

try {

CopyData(“D:\IO\dpy.txt”);

} catch (IOException e) {

e.printStackTrace();

}

}

public static void CopyData(String outpath) throws IOException {

FileWriter fw=new FileWriter(outpath);

BufferedWriter bw=new BufferedWriter(fw);

bw.write(“大忽悠和小朋友”);

bw.flush();

bw.close();

fw.close();

}

}

在这里插入图片描述


缓冲字符流复制文件

========================================================================

public class test {

public static void main(String[] args) {

try {

CopyData(“D:\IO\dpy.txt”,“D:\IO\copy\dpy.txt”);

} catch (IOException e) {

e.printStackTrace();

}

}

public static void CopyData(String inpath,String outpath) throws IOException {

BufferedReader br=new BufferedReader(new FileReader(inpath));

BufferedWriter bw=new BufferedWriter(new FileWriter(outpath));

char[] c=new char[10];

int len=0;

while((len=br.read©)!=-1)

{

bw.write(c,0,len);

}

bw.flush();

bw.close();

br.close();

}

}

在这里插入图片描述


注意:缓冲流是把数据缓冲到内存中


注意:如果向一个.txt里面写入数据,那么默认会覆盖掉里面原来的数据,即删除掉原有数据,来写入新数据,而不是追加写入



转换流

==================================================================

在这里插入图片描述

public class test {

public static void main(String[] args) {

try {

CopyData(“D:\IO\dpy.txt”,“D:\IO\copy\dpy.txt”);

} catch (IOException e) {

e.printStackTrace();

}

}

public static void CopyData(String inpath,String outpath) throws IOException {

//转换输入流

FileInputStream fs=new FileInputStream(inpath);

//把字节流转换为字符流

InputStreamReader in=new InputStreamReader(fs,“UTF-8”);//参数1是字节流,参数2是编码格式

char[] c=new char[100];

int len=0;

while((len=in.read©)!=-1)

{

System.out.println(“字节流转换为字符流:”+new String(c,0,len));

}

in.close();

fs.close();

//转换输出流

FileOutputStream out=new FileOutputStream(outpath);

//把字节输出流转换为字符输出流

OutputStreamWriter os=new OutputStreamWriter(out,“UTF-8”);

os.write(“12345678”);

os.flush();

os.close();

out.close();

}

}

在这里插入图片描述


注意:在转换字符流的时候,设置的字符集编码要与读取的文件的数据的编码格式一致,不然乱码



标准输入和输出流

=======================================================================

在这里插入图片描述

public class test {

public static void main(String[] args) {

try {

CopyData();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void CopyData() throws IOException {

//创建一个接收键盘输入的输入数据的输入流

InputStreamReader is=new InputStreamReader(System.in);

//把输入流放到缓冲流中

BufferedReader br=new BufferedReader(is);

//缓冲字符输出流

BufferedWriter bw=new BufferedWriter(new FileWriter(“D:\IO\dpy.txt”));

String line="";

while((line=br.readLine())!=null)

{

if(line.equals(“over”))

break;

//读取的每一行都写入到指定的txt文件

bw.write(line);

}

bw.flush();

bw.close();

br.close();

is.close();

}

}

在这里插入图片描述

标签:主从复制,简书,String,len,static,new,close,JAVA,public
来源: https://blog.csdn.net/m0_63174475/article/details/121303595

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有