A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 825176857 中级黑马   /  2015-6-18 13:18  /  491 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

一、常用的IO方法
一、File的基本方法
File.separator  分割符
File.pathseparator 路径分割符
boolean  File.exists()   文件是否存在
boolean  File.createNewFile(); 文件是否创建成功
boolean  File.delete();文件是否删除
File.getParent 文件上级目录
boolean  File.isDirectory();判断路径是否是文件夹
boolean  File.isFile();是否是文件
String[] f=File.list(); 文件夹里不完整路径的文件名(不完整 如:音乐)
File[] file=file1.listFiles();文件夹里有完整路径的文件名(完整 如:F:\\音乐)
file.getname; 得到文件名
file.length; 得到文件大小
getAbsolutePath() 得到绝对路径、全路径。
getpath 得到缩写的路径,根据当前目录位置可以缩写路径。得到相对路径。
getCanonicalPath() 得到标准路径,将统一平台间的路径写法差异。
boolean File mkdirs(); 创建文件夹
File.renameTo(newfile);重命名
  二、InputStream的基本方法
    int read() throws IOException 读取一个字节以整数形式返回,如果返回-1已到输入流的末尾
    void close() throws IOException 关闭流释放内存资源
    long skip(long n) throws IOException 跳过n个字节不读
    三、OutputStream的基本方法
    void write(int b) throws IOException 向输出流写入一个字节数据
    void flush() throws IOException 将输出流中缓冲的数据全部写出到目的地
    四、Writer的基本方法
    void write(int c) throws IOException 向输出流写入一个字符数据
    void write(String str) throws IOException将一个字符串中的字符写入到输出流
    void write(String str,int offset,int length)
    将一个字符串从offset开始的length个字符写入到输出流
    void flush() throws IOException
    将输出流中缓冲的数据全部写出到目的地
   五、Reader的基本方法
    int read() throws IOException 读取一个字符以整数形式返回,如果返回-1已到输入流的末尾

二、IO使用的一般原则
一、按数据格式分:  

1、二进制格式(只要不能确定是纯文本的): InputStream, OutputStream及其所有带Stream结束的子类  

2、纯文本格式(含纯英文与汉字或其他编码方式);Reader, Writer及其所有带Reader, Writer的子类  

二、按输入输出分:  

1、输入:Reader, InputStream类型的子类   

2、输出:Writer, OutputStream类型的子类  

三、按缓冲分:   

1、缓冲流:BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter   

四、按是否格式化输出分:   

1、格式化输出:PrintStream, PrintWriter      

五、特殊需要:   

1、从Stream到Reader,Writer的转换类:InputStreamReader, OutputStreamWriter   

2、对象输入输出:ObjectInputStream, ObjectOutputStream   

3、进程间通信:PipeInputStream, PipeOutputStream, PipeReader, PipeWriter  

4、合并输入:SequenceInputStream  

5、更特殊的需要:PushbackInputStream, PushbackReader, LineNumberInputStream, LineNumberReader  
三、常用的实例
一、文件拷贝
import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

public class DemoCopy {

public static void main(String[] args){

String oldPath="F:\\java.doc";

String newPath="E:\\java.text";

copyFile(oldPath,newPath);

}

public static void copyFile(String oldPath, String newPath) {

      try {

          int bytesum = 0;

          int len = 0;

          File oldfile = new File(oldPath);

          if (oldfile.exists()) {  

              InputStream inStream = new FileInputStream(oldPath);

              FileOutputStream fs = new FileOutputStream(newPath);

              byte[] buffer = new byte[1024];

              while ( (len = inStream.read(buffer)) != -1) {

                  bytesum += len;

                  System.out.println(bytesum);

                  fs.write(buffer, 0, len);

              }

              inStream.close();

          }

      }

      catch (Exception e) {

      System.out.println("出错");

          e.printStackTrace();

      }

  }

}
二、查找指定的文件里的文件
import java.io.File;

import java.util.Scanner;

public class test {

public static void find(File f,String extname){

if(f==null){

return ;

}

if(f.isDirectory()){

File[] fs=f.listFiles();

if(fs!=null){

for (File fi : fs) {

find(fi,extname);

}

}

}else{

String path=f.getPath();

if(path.endsWith(extname)){

System.out.println(f.getPath());

}

}



}

public static void main(String[] args){

System.out.println("请输入一个文件目录");

Scanner input=new Scanner(System.in);

String targe=input.next();

File f=new File(targe);

String extname=".jpg";

find(f,extname);

}

}
三、图片上传
客户端:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;


public class Client {


/**
* 客户端
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
Scanner input=new Scanner(System.in);
Socket s=new Socket("192.168.0.104",8008);
System.out.println("输入上传的图片地址");
String target=input.next();
FileInputStream fs=new FileInputStream(target);
OutputStream os=s.getOutputStream();
int len=0;
// int bytesum=0;
byte[] buffer = new byte[1024];
        while ( (len = fs.read(buffer)) != -1) {
//            bytesum += len;
//            System.out.println(bytesum);
            os.write(buffer, 0, len);
        }
        s.shutdownOutput();
        InputStream is=s.getInputStream();
        byte[] b=new byte[1024];
        int num=is.read(b);
        System.out.println(new String(b,0,num));
        fs.close();
        s.close();
}


}
服务器:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class Server {


/**
* 服务器
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ServerSocket ss =new ServerSocket(8008);
System.out.println("服务器启动");
System.out.println("等待连接....");
Socket s=ss.accept();
String address=s.getInetAddress().getHostAddress();
System.out.println(address+"已连接");
InputStream is=s.getInputStream();
FileOutputStream fs=new FileOutputStream("e:\\"+address+".jpg");
byte[] buffer=new byte[1024];
   int len=0;
   while((len=is.read(buffer))!=-1){
         fs.write(buffer,0,len);
   }
   OutputStream out=s.getOutputStream();
   out.write("上传成功了".getBytes());
   fs.close();
   ss.close();
   s.close();


}
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马