io流的输入,字节流inputStream,outputStream的输入与输出,字符流的输入与输出 FileInputStream,FileOutputStream

package io;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class SystemDemo {
public static void main(String[] args) {
SystemDemo sd = new SystemDemo();
//sd.getNameAndPassword();
//sd.getFileInputStreamDemo();
//sd.getFileOutputStreamDemo();
sd.CopyFile();
}
public void getNameAndPassword(){
//确定是读 的方式 System.in
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);

try {
System.out.println("请输入您的姓名:");
//一行一行的读
String name = br.readLine();

System.out.println("请输入您的密码:");
Integer password = Integer.parseInt(br.readLine());

System.out.println("您输入的用户名为:"+name+"密码为:"+password);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getFileInputStreamDemo(){
/*
* 输入inputStream   输出流outputStream  以1byte的方式去读或写入  byte[]
*   字节流      stream :用于进行文件的复制粘贴和处理二进制数据----》图片/视频、音频
*   
*  字符流     用于操作文本文件的内容,例如添加删除修改里面的内容。---->(txt/java/jsp/xml)
* 以2个byte的单位进行读取和写入    ----》char[]

* 缓冲流     叫你读写更高效
*  
* */
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
//创建一个文件对象
//File file = new File("src/1.txt");
File file = new File("src/wicket-xhtml1.4-strict.dtd");
try {
//创建字节流文件输入流对象
inputStream = new FileInputStream(file);
//用read()方法去读
//int num = inputStream.read();
int len = 0 ;
//当读到最后一个的时候是-1
/*while ((len=inputStream.read())!=-1) {
System.out.print((char)len+"");

}*/
byte[] bt = new byte[1024];
while ((len=inputStream.read(bt))!=-1) {
System.out.print(len+"");
//转换的bt数组,开始的数组,结束的数组。这个样子就不会在最后一次也打印 1024个字节了。
System.out.println(new String(bt,0,len));
}


/*//用 read()的 byte[]方式去读
byte[] bs = new byte[1024];   //1025个字节

while((len=inputStream.read(bs))!=-1){
System.out.print(len+" ");
System.out.println(new String(bs));
}
*/
/* byte[] bt = new byte[1024];  //1024 中
long total = file.length();//1028
float num = 0f;
while((len=inputStream.read(bt))!=-1){
System.out.println("======"+len);
num += len;
System.out.println(new String(bt));
//数字格式化
System.out.println(new DecimalFormat("#.##").format(num/total));
System.out.println(num/total);
}*/
} catch (Exception e) {
e.printStackTrace();
}
}

public void getFileOutputStreamDemo(){
//字节输出流
File file = new File("src/test.txt");
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
String contents = "qweqwe李薇薇"; 
byte[] bt = contents.getBytes(); 
//OutputStreamWriter out = new OutputStreamWriter(outputStream, "utf-8");
outputStream.write(bt);
//out.write(contents);
} catch (Exception e) {
e.printStackTrace();
}finally{
System.out.println("创建文件test.dtd成功!!!");
try {
if(outputStream!=null)outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

public  void CopyFile(){
//获取要复制的文件路径
File file = new File("C:/Users/Administrator/Documents/Tencent Files/1273091433/FileRecv/大数据云计算架构师.rar");
//存放位置
File file2 = new File("d:/111.rar");
//输入流
FileInputStream in = null;
//输出流
FileOutputStream out = null;
try {
in = new FileInputStream(file);
out = new FileOutputStream(file2);
//byte[]可以存储多少 字节一般都是 读取 1kb 多少没有限制我试了一下 60MB依然可以读取出来。
byte[] b = new byte[1024];
System.out.println("开始复制文件。。。");
//记录时间
Long starTime =  System.currentTimeMillis();
int len = 0;
//开始复制
while ((len=in.read(b))!=-1) {
System.out.println(len+"     ");
out.write(b, 0, len);
}
//复制结束时间
Long lastTime = System.currentTimeMillis();
System.out.println("复制完成!");
System.out.println("一共用时:"+(lastTime-starTime));
} catch (Exception e) {
e.printStackTrace();
}finally{
//开流要记得关流。
try {
if(out!=null)out.close();
if(in!=null)in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值