I/O流与网络编程

I/O流与网络编程

将文件通过流方式输出到控制台上

import org.junit.Test;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileTest {
    //单元测试的文件路径定位到模块下,main方法则定位到工程下
    @Test
    public void test(){
        FileReader fileReader= null;
        try {
            File file = new File("hello.txt");
            fileReader = new FileReader(file);
            int data;
            while ((data=fileReader.read())!=-1){
                System.out.print((char)data);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (fileReader!=null){
                    fileReader.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

使用read(char[] cbuf)读数据

 @Test
    public void test1(){
        FileReader fr = null;
        try {
            File file = new File("hello.txt");
            fr = new FileReader(file);
            int len;
            char[] cbuf=new char[5];
            while ((len=fr.read(cbuf))!=-1){
                //方式一
//                for (int i = 0; i < len; i++) {
//                    System.out.print(cbuf[i]);
//                }
                //方式二
                String s = new String(cbuf, 0, len);
                System.out.print(s);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                fr.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

写出数据到硬盘上

/**
 * FileWriter(File file) 默认是覆盖
 * FileWriter(File file,boolean append) append=true是追加
 */
@Test
public void test2(){
    FileWriter fileWriter = null;
    try {
        File file = new File("hello1.txt");
        fileWriter = new FileWriter(file,true);
        fileWriter.write("I have a dream\n");
        fileWriter.write("You need to have a dream");
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (fileWriter!=null){
            try {
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

}

文件的读入和写出

 @Test
    public void test3(){
        FileReader fr= null;
        FileWriter fw= null;
        try {
            File file = new File("hello1.txt");
            fr = new FileReader(file);
            fw = new FileWriter("hello2.txt");
            char[] cbuf=new char[5];
            int len;
            while ((len=fr.read(cbuf))!=-1){
                fw.write(cbuf,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fr!=null){

                try {
                    fr.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fw!=null){

                try {
                    fw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

图片的复制

 /**
     * 测试FileInputStream和FileOutputStream读写图片文件
     * 字节流数组
     */
    @Test
    public void test4(){
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream= null;
        try {
            fileInputStream = new FileInputStream(new File("MP130S.png"));
            fileOutputStream = new FileOutputStream(new File("MP130S1.png"));
            byte[] buff=new byte[1024];
            int len;
            while ((len=fileInputStream.read(buff))!=-1){
                fileOutputStream.write(buff,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fileInputStream!=null){

                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fileOutputStream!=null){

                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }




	//封装为一个方法
   public void testFile(String srcPath,String desPath) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(new File(srcPath));
            fileOutputStream = new FileOutputStream(new File(desPath));
            byte[] buff = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buff)) != -1) {
                fileOutputStream.write(buff, 0, len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fileInputStream != null) {

                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fileOutputStream != null) {

                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

使用缓冲流对非文本文件的复制

 @Test
    public void test6(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            fis = new FileInputStream(new File("C:\\Users\\CZ\\Desktop\\968cad867de68c74ce135336a947c3f2.mp4"));
            fos = new FileOutputStream(new File("968cad867de68c74ce135336a947c3f21.mp4"));
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            byte[] bytes=new byte[1024];
            int len;
            while ((len=bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (bis!=null){

                try {
                    bis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (bos!=null){

                try {
                    bos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fis!=null){

                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fos!=null){

                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }

使用缓冲流对文本文件的复制

 @Test
    public void test7(){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(new File("hello1.txt")));
            bw = new BufferedWriter(new FileWriter(new File("hello4.txt")));
            char[] cbuf=new char[5];
            int len;
            while ((len=br.read(cbuf))!=-1){
                bw.write(cbuf,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (br!=null){

                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (bw!=null){

                try {
                    bw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

对图片进行加密和解密

 /**
     * 将读到的值进行异或,如果再异或一遍就是解密
     */
    @Test
    public void test8(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(new File("MP130S.png"));
            fos = new FileOutputStream(new File("MP130S3.png"));
            byte[] bytes=new byte[1024];
            int len;
            while ((len=fis.read(bytes))!=-1){
                for (int i = 0; i < len; i++) {
                    bytes[i]= (byte) (bytes[i] ^ 5);
                }
                fos.write(bytes,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fis!=null){

                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fos!=null){

                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

对每个字符出现的次数进行计数

 @Test
    public void test1(){
        FileReader fr = null;
        FileWriter fw= null;
        try {
            fr = new FileReader(new File("zj.txt"));
            Map<Character,Integer> map=new HashMap<>();
            int len;
            while ((len=fr.read())!=-1) {
                char c = (char) len;
                if (map.get(c) == null) {
                    map.put(c, 1);
                } else {
                    map.put(c, map.get(c) + 1);
                }
            }
            fw = new FileWriter(new File("zj1.txt"));
                Set<Map.Entry<Character, Integer>> entries = map.entrySet();
                for (Map.Entry<Character,Integer> entry1:entries){
                    switch (entry1.getKey()){
                        case ' ':
                            fw.write("空格="+entry1.getValue());
                            break;
                        case '\t':
                            fw.write("tab键="+entry1.getValue());
                            break;
                        case '\r':
                            fw.write("回车="+entry1.getValue());
                            break;
                        case '\n'
                                :fw.write("换行="+entry1.getValue());
                            break;
                        default:
                            fw.write(entry1.getKey()+"="+entry1.getValue());
                            break;
                    }
                    fw.write("\n");
                }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fr!=null){

                try {
                    fr.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fw!=null){

                try {
                    fw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

标准输入输出流的练习

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileTest3 {

    public static void main(String[] args) {
        InputStreamReader isr = null;
        BufferedReader br= null;
        try {
            isr = new InputStreamReader(System.in);
            br = new BufferedReader(isr);

            while (true){
                System.out.println("请输入字符串");
                String data = br.readLine();
                if ("e".equalsIgnoreCase(data)||"exit".equalsIgnoreCase(data)){
                    System.out.println("程序结束");
                    break;
                }
                String s = data.toUpperCase();
                System.out.println(s);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (br!=null){

                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (isr!=null){

                try {
                    isr.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}

网络编程

客户端发送信息给客户端,服务端显示信息在控制台

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPTest {



    @Test
    public void client(){
        Socket socket= null;
        OutputStream os = null;
        try {
            //客户端的ip地址
            InetAddress ip=InetAddress.getByName("127.0.0.1");
            //创建Socket类参数为ip和端口的对象
            socket = new Socket(ip,8899);
            //输出流
            os = socket.getOutputStream();
            //返送信息
            os.write("你好,我是客户端,我正在给你发信息".getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (os!=null){

                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (socket!=null){

                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }


    @Test
    public void server(){
        ServerSocket ss= null;
        InputStream is = null;
        Socket socket=null;
        ByteArrayOutputStream baos= null;
        try {
            //通过构造器创建端口号
             ss = new ServerSocket(8899);
             //通过调用accept()创建Socket对象
             socket = ss.accept();
             //获取输入流
            is = socket.getInputStream();
//            将数据写出
            baos = new ByteArrayOutputStream();
            byte[] bytes=new byte[5];
            int len;
            while ((len=is.read(bytes))!=-1){
                baos.write(bytes,0,len);
            }
            System.out.println(baos.toString());
            //获取接收到的ip地址
            System.out.println("接收信息的ip地址为:"+socket.getInetAddress().getHostAddress());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (baos!=null){

                try {
                    baos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (is!=null) {

                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (socket!=null){

                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (ss!=null){
                try {
                    ss.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }

    }
}

客户端发送文件给服务器端,服务器端存在本地

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPTest1 {

    @Test
    public void client(){

        Socket socket= null;
        OutputStream os = null;
        FileInputStream fis= null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
            os = socket.getOutputStream();
            fis = new FileInputStream("MP130S.png");
            byte[] bytes=new byte[1024];
            int len;
            while ((len=fis.read(bytes))!=-1){
                os.write(bytes,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fis!=null){

                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (os!=null){

                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (socket!=null){

                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }


    @Test
    public void server(){

        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos= null;
        try {
            ss = new ServerSocket(9090);
            socket = ss.accept();
            is = socket.getInputStream();
            fos = new FileOutputStream("MP130S1.png");
            byte[] bytes=new byte[1024];
            int len;
            while ((len=is.read(bytes))!=-1){
                fos.write(bytes,0,len);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fos!=null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (is!=null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (socket!=null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (ss!=null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}

客户端发送文件给服务器端,服务器端存在本地,客户端再发送数据给服务器端,服务器端接收并输出

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPTest2 {

    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis= null;
        InputStream is=null;
        ByteArrayOutputStream baos=null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 8999);
            os = socket.getOutputStream();
            fis = new FileInputStream("MP130S.png");
            byte[] bytes=new byte[10];
            int len;
            while ((len=fis.read(bytes))!=-1){
                os.write(bytes,0,len);
            }
            //read()会堵塞,此时进行关闭
            socket.shutdownOutput();

            //接受服务端传来的数据
            is = socket.getInputStream();
            baos=new ByteArrayOutputStream();
            byte[] bytes1=new byte[10];
            int len1;
            while ((len1=is.read(bytes1))!=-1){
                baos.write(bytes1,0,len1);
            }

            System.out.println(baos.toString());

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fis!=null){

                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (os!=null){

                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }
        }
    }
    @Test
    public void server(){
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos= null;
        OutputStream os = null;
        try {
            ss = new ServerSocket(8999);
            socket = ss.accept();
            is = socket.getInputStream();
            fos = new FileOutputStream("MP130S2.png");
            byte[] bytes=new byte[50];
            int len;
            while ((len=is.read(bytes))!=-1){
                fos.write(bytes,0,len);
            }

            //像客户端发送数据表示已经收到
            os = socket.getOutputStream();
            os.write("客户端,我这边已经收到你传输的数据".getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (os!=null){

                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (is!=null){

                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (socket!=null){

                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (ss!=null){

                try {
                    ss.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值