IO流

IO流:   方向分为:输入流:读取数据 
输出流:写输出数据
数据类型分为:先有的字节流,然后有了字符流.
 针对文本文件操作:优先字符流.用记事本打开,能看懂
 字节流:图片文件,音频,视频…
  字节输入流:InputSream: 抽象类:FileInputStream
  字节输出流:OutputStream:抽象类 FileOutputStream
   复制方法:1.单个字节遍历(最低)
           2.一次便利一个字节数组(2)
           3.缓冲流:1.一次一个(3)  2.一个一个字节数组(效率最高)  
  字符输入流:Reader:FileReader
  字符输出流:Writer:FileWriter
  
 字节输入流:一次遍历一个字节
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
FileInputStream fI = new FileInputStream("first.txt");
    //字节输入流
int by=0;
while((by=fI.read())!=-1){
System.out.print((char)by+" ");
}
fI.close();
}
}


一个中文转换成两个字节:默认GBK格式
Utf-8:一个字节转换成三个字符
public class StringDemo {
public static void main(String[] args) {
String s="卢本伟牛逼";
byte[]bys=s.getBytes();
System.out.println(Arrays.toString(bys));
}
}



字节输入流:一次遍历一个字节数组
public class FileInputStreamDemo2 {
public static void main(String[] args) throws IOException {
//创建文件字节输入流对象
FileInputStream fI = new FileInputStream("second.txt") ;
    byte[]by=new byte[1024];
    int len=0;
    while((len=fI.read(by))!=-1){
    System.out.println(new String(by,0,len));
    }
}
}


//将third.txt复制到four.txt
数据源: third.txt读数据
目的地: four.txt写数据


public class FileInputDemo3 {
public static void main(String[] args) throws IOException {
   // 文件字节输入流
FileInputStream fs=new FileInputStream("third.txt");
   // 文件字节输出流
   FileOutputStream fo=new FileOutputStream("four.txt");
   int by=0;
   while((by=fs.read())!=-1){
  fo.write(by);
  fo.flush();
   }
   fs.close();
   fo.close();
}
}


一次遍历单个字节复制图片
public class FileInputStreamDemo5 {
public static void main(String[] args) throws IOException {
FileInputStream fl = new FileInputStream("3.png");
    FileOutputStream fo = new FileOutputStream("4.png");
    int len=0;
    while((len=fl.read())!=-1){
    fo.write(len);
    }
    fl.close();
    fo.close();
}
}


一次遍历一个字节数组.
//复制图片
public class FileInputDemo4 {
public static void main(String[] args) throws IOException {
FileInputStream fs= new FileInputStream("e:\\2.png");
    FileOutputStream so = new FileOutputStream("3.png");
    //一次便利一个字节数组
    int by=0;
   byte[]b=new byte[1024];
    while((by=fs.read())!=-1){
    so.write(b, 0, by);
    }
    fs.close();
    so.close();
}
}



字符缓冲流:一次遍历一个字节数组.输出结果
public class FileInputStreamDemo6 {
public static void main(String[] args) throws IOException {
//创建一个字节缓冲输入流对象
BufferedInputStream bs = new BufferedInputStream(new FileInputStream("four.txt"));
int len=0;
byte []b=new byte [1024];
while((len=bs.read(b))!=-1){
System.out.println(new String(b,0,len));
}
bs.close();
}
}




Flush()和close()方法区别
Flush():刷新缓冲区的流,强迫将缓冲字节都写到流中,刷新之后可以写数据的
Close():关闭流对象指向这个资源文件,一旦关闭,写数据进不去.


计算机如何识别中文的?
GBK模式:每个中文左边对象字节一定是负数
GBK格式:右边的字节可以中正数,可以是0,可以是负数
一个汉字转换两个字节


public class FileDemo {
public static void main(String[] args) {
String s = "英雄联盟" ;
byte[] bys = s.getBytes() ;System.out.println(bys);
System.out.println(Arrays.toString(bys));
}
}


//转换成字节: 
[-45, -94, -48, -37, -63, -86, -61, -53]


字节流一次读取一次字节,出现中文乱码:
给字节进行强制类型转换,当中文时需要使用字符流.默认GBK格式
字符流必须指定编码格式


编码:就是将能看懂的字符串转换成看不懂的字节数组
public byte[] getBytes(Charset charset):将字符串转换成字节数组,指定编码格式(字符集)


解码:就是将看不懂的字节数组看懂的:将字节数组字符串
public class FileDemo2 {
public static void main(String[] args) throws IOException {
//编码
String s = "我是龙哥" ;
byte[] by = s.getBytes() ;
System.out.println(Arrays.toString(by));
//解码
String st=new String(by,"GBK");
System.out.println(st);

}
}


输出结果:
[-50, -46, -54, -57, -63, -6, -72, -25]
我是龙哥



字符输入流:字节输入流+编码格式


字符输出流:Writer:是一个抽象类
使用子类:OutputStreamWriter字符转换流:可以将字节输出流转换字符流


public OutputStreamWriter(OutputStream out,Charset cs)   后面的指定编码格式
publicStreamWriter(OutputStream out) //使用默认GBK编码
eg:
public class InputStreamDemo {
public static void main(String[] args) throws IOException  {
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(
"second.txt"),"GBK"); //原有的写
OutputStreamWriter writer2 = new OutputStreamWriter(new FileOutputStream(
"six.txt"));//默认GBK //新建一个
writer.write("10");
writer.close();
writer2.write("本宝宝");
writer2.close();
}



将一个项目下内容复制.
public class InputDemo {
public static void main(String[] args) throws IOException {
//数据源:a.txt---->Reader---->InputStreamReader(InputStream in):字符转换输入流
//目的地:b.txt---->Writer---->OutputStreamWriter(Outputstream out):字符转换输出流
InputStreamReader isr = new InputStreamReader(new FileInputStream(
"first.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
"b.txt"));
char[] chs = new char[1024] ;
int len = 0;
while((len=isr.read(chs))!=-1){
//写数据:
osw.write(chs, 0, len) ;
//刷新流
osw.flush() ;
}
isr.close() ;
osw.close() ;


}
}




使用字符转换流进行操作数据的时候:字节流+编码格式
新的:便捷类.


public class FileReaderDemo1 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("four.txt");
    FileWriter fw = new FileWriter("nine.txt");
    int len=0;
   char []b=new char[1024];
    while((len=fr.read(b))!=-1){
    fw.write(b, 0, len);
fw.flush() ;
    }
    fw.close() ;
fr.close() ;
}
}


符输出流写数据的方法:
public void write(int c):写单个字符
public void write(char[] cbuf):写一个字符数组
public abstract void write(char[] cbuf,int off,int len):写入字符数组的一部分
public void write(String str):写字符串
public void write(String str, int off,int len):写字符串的一部分




字符缓冲流:特有功能
BufferWriter:public void newline():写入换行字符
BufferReader:public String readLine():一次读取一行




public class FileDemo {
public static void main(String[]args)throws IOException {
//读数据
//创建一个字符缓冲输入流对象  读数据
BufferedReader br = new BufferedReader(new FileReader("four.txt")) ;
String line = null ;
while((line=br.readLine())!=null){
System.out.println(line);
}
}
}


public class FileDemo3 {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("ten.txt")) ;
//写数据
for(int x = 0 ; x <10 ; x ++){
bw.write("龙哥"+x+"厉害") ;
bw.newLine() ;
bw.flush() ;
}
bw.close();
}
}


没有  newline()前,写入换行符号(“\r\n”):

一次读取一个字节,一次读取一个字节数组方法差不多.


递归三个条件
1. 定义一个方法,
2. 必须要有规律
3. 要有出口条件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
牙科就诊管理系统利用当下成熟完善的SSM框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的Mysql数据库进行程序开发。实现了用户在线查看数据。管理员管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等功能。牙科就诊管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 管理员在后台主要管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等。 牙医列表页面,此页面提供给管理员的功能有:查看牙医、新增牙医、修改牙医、删除牙医等。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。药品管理页面,此页面提供给管理员的功能有:新增药品,修改药品,删除药品。药品类型管理页面,此页面提供给管理员的功能有:新增药品类型,修改药品类型,删除药品类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值