JAVA SOCKET的I/O 使用

JAVA SOCKET的I/O 使用 html

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
PrintWriter out = new PrintWriter(socket.getOutputStream(),true); out.close(); in.close();

使用中,又出現問題:
readLine一直讀不出東西來,使用read有int;
解決辦法: java

out.println(s); out.flush();

強調:必定要使用println,不能是print,readLine是根據換行來判斷是否結束,不然讀不出來。 socket

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 spa

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();
			}

		}
	}
}
相關文章
相關標籤/搜索