第十四章作业
来源:互联网 发布:cygwin linux内核 编辑:程序博客网 时间:2024/05/17 01:10
第三题
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
- LDD 第十四章 作业
- 作业第十四章
- 第十四章课后作业
- 第十四章作业
- 第十四章作业
- python 教程 第十四章、 地址薄作业
- python 教程 第十四章、 地址薄作业
- 李红霞作业第三章第十四题
- 第十四章
- 第十四章
- 第十四章
- 第十四章
- 第十四章
- 第十四章
- 《Java数据结构和算法》第二版 Robert lafore 编程作业 第十四章
- Windows编程基础 第十四章 进程 作业 线程 线程局部存储
- 第十四作业-2java集合框架
- 第十四章 链表
- Linux下利用SWIG完成Python对使用OpenCV的C++文件的调用
- hdu oj 1961 Pseudo-Random Numbers
- [Java数据结构]简单排序之选择排序
- C/C++初学攻略
- 开篇宣言
- 第十四章作业
- Party Games UVA
- Angular历险记
- shiro--奇怪的不能登录问题
- PAT 乙级 1071. 小赌怡情(15)
- Go基础
- 简单背包问题(0032)
- Python 练习实例27
- 位操作入门