IO总结

流的概念:java中输入输出相关的类都是java.io包中,java将输入和输出抽象定义为流
流的分类:
流按方向分为输入流(InputStream)和输出流(OutpubStream)
流按性质分:
1.基础字节流(原始流):InputStream 和 OutputStream是java中可以按最小数据单位读取的流。
基础流是直接连接到输入源的流
InputStream:
InputStream的重要方法:
int available():流中可读取的有效字节的长度,如果InputStream的对象源是文件,则表示文件中可读取的有效字节的长度。
void close():流对象使用完要关闭。
int read():这个方法调用会返回流中的下一个字节作为一个byte值,如果读到末尾,返回-1。
InputStream是抽象类,用它的实现类FileInputStream创建对象
public String read(String filename)throws Exception{
//构造输入流对象
java.io.InputStream ins=new java.io.FileInputStream(filename);
//通过文件对象构造输入流对象
// File name=new File(filename);
// java.io.InputStream ins=new java.io.FileInputStream(name);
//根据可读取字节长度定义byte数组
byte[] array=new byte[ins.available];
//将流中的数据读到数组中
ins.read(array);
// int i=-1;int count=0;
// while((i=ins.read())!=-1){
// array[count++]=(byte)i;
// }
//将byte数组转换成字符串
String s=new String(array);
return s;
}
OutputStream:
OutputStream的重要方法:
void close():关闭流
void flush():将输入流中有可能还保存在内存中的内容强制输出到目标中
void write(byte[] b):将byte数组中的内容输出到流中
void write(byte[] b,int off,int len):将数组中的一部分写到流中
void write(int b):向流中写入一个byte值
按字节读取文件:
public boolean(String srcFile,String destFile)throws Exception{
//创建输入流
InputStream ins=new FileInputStream(srcFile);
//创建输出流
OutputStream ous=new FileOutputStream(destFile);
int i=-1;
//从流中读取一个字节
while((i=ins.read())!=-1){
//将这个字节写入到流中
ous.write(i);
}
//从流中读取多个字节
byte[]array =new byte[10];
while ((i=ins.read())!=-1){
ous.write(array,0,10);
}
ins.close();
ous.flush(); //将内存中的数据强制写出
ous.close();
return true;
}
2.过滤流(节点流):用来包装基础流以提供更好的特性。BufferedInputStream和BufferedOutputStream
并不直接连到数据源
好处:读写速度快
public String read(String filename,String dirname)throws Exception{
//构造输入流对象
InputStream ins=new FileInputStream(filename);
BufferedInputStream bis=new BufferedInputStream(ins);
//构造输出流对象
OnputStream out=new FileOnputStream(dirname);
BufferedOutputStream bos=new BufferedOnputStream(out);
...
}
3.基与具体数据类型的流:如果要从流中读取指定类型的数据,要使用DataInputStream和DataOutputStream
public String read(String filename,String dirname)throws Exception{
//构造输入流对象
InputStream ins=new FileInputStream(filename);
DataInputStream dis=new DataInputStream(ins);
//构造输出流对象
OnputStream out=new FileOnputStream(dirname);
DataOutputStream dos=new DataOnputStream(out);
dos.writeBoolean(true);
dos.writeByte((byte)123);
dos.writeChar('j');

dis.readBoolean();
dis.readByte();
dis.readChar();
...
}
4.基与对象的读写:即对象的输入输出流,ObjectInputStream和ObjectOutputStream
public String read(String filename,String dirname)throws Exception{
//构造输入流对象
InputStream ins=new FileInputStream(filename);
ObjectInputStream input=new ObjectInputStream(ins);
//构造输出流对象
OnputStream out=new FileOnputStream(dirname);
ObjectOutputStream output=new ObjectOutputStream(out);
...
}
与File的结合,实现一个查找电脑上的文件,当输入一个关键字时,显示电脑上含有此关键字的文件(文件名或文件内容含有此关键字)
思路:通过搜索区的路径建立File对象,并建立File[]数组返回它下级所有的文件和目录,进行判断,如果是目录,判断此目录的路径名
是否含有此关键字(用String类中的indexOf()方法),如果含有则输出此目录,并查找它下一级的目录和文件(递归)。如果是文件,
判断此文件名是否含有此关键字,如果含有则输出,如果不含有通过其地址名对其内容进行查找,判断是否含有此关键字,如果含有则返回。
代码:
//返回文件内容
public String reacherfile(String path){
//构造输入流对象
FileInputStream fis=new FileInputStream(path);
BufferedInputStream bis=new BufferedInputStream(fis);
//创建byte数组
byte[] array=new byte[ins.available()];
//读文件
bis.read(array);
//关闭流
bis.close();
String s=new String (array);
return s;
}
//统计文件个数
public int printlist(String s,String s2) {
int count=0;
File file=new File(s);
File[] subFile=file.listFiles();
if(subFile==null||subFile.length==0){
return 0;
}
for(int i=0;i<subFile.length;i++){
if(subFile[i].isDirectory()){
//如果是目录,得到绝对路径
String subpath=subFile[i].getAbsolutePath();
// System.out.println("目录:"+subpath);
if(subpath.indexOf(s2)>=0){ //如果目录中含有要查找的字符串,则输出目录
ja.append("目录:"+subpath+"\n");
}
count+=printlist(subpath,s2);
}
if(subFile[i].isFile()){
//如果是文件,得到文件名
count++;
String path=subFile[i].getAbsolutePath();
if(path.indexOf(s2)>=0){ //如果文件路径中含有要查找的字符串,输出文件
ja.append("文件:"+path+"\n");
}else{
String a = null;
try {
a = copyFile(path);
} catch (Exception e) {
e.printStackTrace();
}
if(a.indexOf(s2)>=0){ //如果文件内容中含有要查找的字符串,输出文件
ja.append("文件:"+path+"\n");
}
}
}
}
return count;
}
画图板的保存和打开:
首先在原有基础上加菜单栏
public JMenuBar createMenu(){
//创建一个菜单栏对象
JMenuBar mb=new JMenuBar();
String []arraymenu={"文件","颜色","帮助"};
String [][]arrayitem={ { "打开", "保存", "退出" }, { "编辑颜色" },{ "关于画图", "作者" } };
for(int i=0;i<arraymenu.length;i++){
JMenu menu=new JMenu(arraymenu[i]);
for(int j=0;j<arrayitem[i].length;j++){
JMenuItem item=new JMenuItem(arrayitem[i][j]);
item.addActionListener(al);
menu.add(item);
}
}
return mb;
}
当点击保存时,要将画布上现有的图案保存。
1 建立一个ScreenCapture类,进行截屏。
public class ScreenCapture {
public int [][]createScreenCapture(JPanel draw) throws AWTException{
//首先获取画布的宽和高
int width=draw.getWidth(); //返回double类型
int height=draw.getHeight(); //返回double类型
//获取画布在屏幕上的位置
Point point=draw.getLocationOnScreen();
System.out.println("x:"+point.getX()+" y:"+point.getY());
//实例化一个矩形对象
Rectangle rect=new Rectangle((int)point.getX(),(int)point.getY(),width,height);
//实例化一个Robot对象
Robot robot=new Robot(); //要抛出异常,要进行处理
//截屏
BufferedImage image=robot.createScreenCapture(rect);
//定义二维数组来存放像素点
int array[][]=new int [width][height];
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
//将像素点存放到二维数组中
array[i][j]=image.getRGB(i,j);
}
}
return array;
}
2 实例化输出流,将二维数组中的数据写入文件
写头文件,写文件内容。
public boolean Save(String path,int array[][]){
boolean state=false;
try{
//创建输出流对象
OutputStream os=new FileOutputStream(path);
BufferedOutputStream bos=new BufferedOutputStream(os);
DataOutputStream dos=new DataOutputStream(bos);
//写头文件
dos.writeInt(array.length); //写文件的宽度
dos.writeInt(array[0].length); //写文件的高度
//写文件内容
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
dos.writeInt(array[i][j]);
}
}
//关闭流
dos.close();
bos.close();
os.close();
state=ture;
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
return state;
}
点击打开时,根据所给地址重绘图案
实例化输入流对象,将数据写入二维数组中,然后进行重绘
public int array[][]Open(String path){
int array[][]=null;
try{
//创建输入流对象
InputStream is=new FileInputStream(path);
BufferedInputStream bis=new BufferedInputStream(is);
DataInputStream dis=new DataInputStream(bis);
//读头文件信息
int width=dis.readInt();
int height=dis.readInt();
//创建数组来存放读取的信息
array=new int[width][height];
for(int i=0;i<width;i++){
for(int j=0;j<height;j++){
array[i][j]=dis.readInt();
}
}
//关闭流
dis.close();
bis.close();
is.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
//返回数组
return array;
}
动作监听器中的设置
private ActionListener al=new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand=="保存"){
//实例化ScreenCapture对象
ScreenCapture sc=new ScreenCapture();
int array[][]=sc.createScreenCapture(draw);
//输入要保存到的地址
String path="...";
//实例化一个DrawOpenSave对象
DrawOpenSave dos=new DrawOpenSave();
//调用DrawOpenSave中的Save方法
if(dos.Save(path,array){
JOptionPane.showMessageDialog(null,"保存成功!");
}else if(e.getActionCommend=="打开"){
//实例化一个DrawOpenSave对象
DrawOpenSave dos=new DrawOpenSave();
String path="C:/Users/aa/Desktop/蓝杰 java/drawing.abc";
int array[][]=dos.Open(path);
draw.setArray(array);
draw.repaint();
}
}
}
if(array!=null){
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
g.setColor(new Color(array[i][j]));
g.drawLine(i,j,i,j);
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值