JAVA SOCKET的I/O 使用
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream(),true); out.close(); in.close();
使用中,又出现问题:
readLine一直读不出东西来,使用read有int; 解决办法:out.println(s); out.flush();
强调:一定要使用println,不能是print,readLine是根据换行来判断是否结束,否则读不出来。
ByteArrayOutputStream baos=new ByteArrayOutputStream(); byte[] buffer=new byte[1024]; int len=-1;while((len = is.read(buffer)!)=-1){ baos.write(buffer,0,len);}is.close();String html =baos.toString();baos.close(); return html;
java写入txt
public class NewFile { public static void createFile(File file) { if (!file.exists()) { try { System.out.println("is join"); file.createNewFile(); } catch (IOException e) { System.out.println("创建文件夹失败,原因:" + e.toString()); } } else { System.out.println("文件已存在"); } System.out.println("********end*******"); } public static void main(String[] args) { String encoding = "GBK"; File file = new File("./test.txt"); createFile(file); if (file.exists()) { System.out.println("*******run*******"); try { BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(new Date().toString()); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }}