第十四章作业

第三题

    static String loadStream(InputStream in) throws IOException {
        BufferedReader bufReader = new BufferedReader(new InputStreamReader(in));
        String str = "", buf;

        while((buf = bufReader.readLine()) != null){
            str += buf;
        }
        in.close();
        return str;
    }

第四题

    static String toUppercase(String sour) throws IOException {
        byte[] buf = sour.getBytes();
        InputStream in = new ByteArrayInputStream(buf);
        OutputStream out = new ByteArrayOutputStream();
        int n = 0;
        while ((n = in.read()) != -1) {
            if (n >= 'a' && n <= 'z')
                n -= 32;
            out.write(n);
        }
        return out.toString();
    }

第五题

    public static String loadFile(String fileName) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
        String out = "", buf;
        while((buf = in.readLine()) != null){
            out += buf + "\n";
        }
        in.close();
        return out;
    }

第六题

    public static boolean saveFile(String fileName, String contents) {
        boolean saved = false;
        try {
            BufferedWriter os = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
            os.write(contents);
            os.close();
            saved = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return saved;
    }

第七题

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

class MyServer{
    ServerSocket server = null;
    Socket socket = null;
    public MyServer() {
        try {
            server = new ServerSocket(4700);
            socket = server.accept();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public boolean saveData(String fileName) {
        boolean saved = false;
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
            String buf;
            while((buf = in.readLine()) != null) {
                out.write(buf);
                out.newLine();
            }
            out.flush();
            in.close();
            out.close();
            saved = true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return saved;
    }
    protected void finalize() throws Throwable {
        super.finalize();
        socket.close();
        server.close();
    }
}
public class Q_7 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new MyServer().saveData("test.txt");
    }

}
测试客户端
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class TalkClient {

    public static void main(String[] args) {
        Socket socket = null;
        try {
            socket = new Socket("127.0.0.1", 4700);
        } catch (IOException e) {
            System.out.println("can't not listen to " + e);
        }
        try {
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            out.write("Monday\nTuesday\nWednesday\nThursday\nFriday\nSaturday\nSunday\n");
            out.flush();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

第八题

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Q_8 {
    private static boolean fileMatch(String pattern, String fileName) {
        int fileNameIndex = 0;
        int fileNameLength = fileName.length();
        for(int patternIndex = 0; patternIndex < pattern.length(); ++patternIndex) {
            char ch = pattern.charAt(patternIndex);
            if(ch == '*') {
                while(fileNameIndex < fileNameLength) {
                    if(fileMatch(pattern.substring(patternIndex + 1), fileName.substring(fileNameIndex)))
                        return true;
                    ++fileNameIndex;
                }
            }else if(ch == '?') {
                for(int i = 0; i < 2; ++i) {
                    if(fileNameIndex < fileNameLength) {
                        if(fileMatch(pattern.substring(patternIndex + 1), fileName.substring(fileNameIndex)))
                            return true;
                    }
                    ++fileNameIndex;
                }
            }else {
                if(fileNameIndex >= fileNameLength || ch != fileName.charAt(fileNameIndex))
                    return false;
                ++fileNameIndex;
            }
        }
        return (fileNameIndex == fileNameLength);
    }
    public static void findFile(String path, String fileName, List<File> fileList) {
        File dir = new File(path);
        if(!dir.exists() || !dir.isDirectory())
            System.out.println("not find " + fileName);
        File tempFile;
        String tempName;
        File[] files = dir.listFiles();
        for(int i = 0; i < files.length; ++i) {
            tempFile = files[i];
            if(tempFile.isDirectory()) {
                findFile(tempFile.getAbsolutePath(), fileName, fileList);
            }else if(tempFile.isFile()) {
                tempName = tempFile.getName();
                if(fileMatch(fileName, tempName))
                    fileList.add(tempFile.getAbsoluteFile());
            }
        }
        if(fileList.isEmpty()) {
            System.out.println("not find " + fileName);
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String path = "D:/test/";
        String fileName = "t?est*.txt";
        List<File> fileList = new ArrayList<>();
        findFile(path, fileName, fileList);
        if(!fileList.isEmpty()) {
            for(File file : fileList)
                System.out.println(file.getAbsolutePath());
        }
    }

}

十五章第六题

服务端
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Ch15Q_6_server {
    private ServerSocket server;
    private Socket socket;
    public Ch15Q_6_server() {
        try {
            server = new ServerSocket(4700);
            socket = server.accept();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    public void accept() {
        try {
            ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
            User u2 = (User) in.readObject();
            System.out.println(u2.getName() + "  " + u2.getPassword());
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    protected void finalize() throws Throwable {
        super.finalize();
        socket.close();
        server.close();
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Ch15Q_6_server().accept();
    }

}
客户端
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Ch15Q_6_client {
    private Socket socket = null;
    public Ch15Q_6_client() {
        try {
            socket = new Socket("127.0.0.1", 4700);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    public void send() {
        try {
            ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
            User u1 = new User();
            u1.setName("zouxiaoqiang");
            u1.setPassword("1234");
            out.writeObject(u1);
            out.flush();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    protected void finalize() throws Throwable {
        super.finalize();
        socket.close();
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Ch15Q_6_client().send();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值