0

我必须制作一个服务器,它从客户端编号作为输入并将其输出到文本文件中。我收到以下错误。
错误是:

在 java.io.BufferedReader.ensureOpen(Unknown Source)
java.io.BufferedReader.read(Unknown Source)
Server_thread.writeTxt(Server_thread.java:72)
Server_thread.run(Server_thread.java:49)
java.lang.Thread.run (来源不明)

和代码

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;



public class Server_thread implements Runnable{
     private Socket socket;
     private BufferedReader  instream=null;
     private BufferedWriter outstream=null;
     private FileWriter file=null;
     private Scanner scanner=null;
     private int number=0;
     private String strin;
        public Server_thread(Socket socket) {
            this.socket = socket;

            Thread t = new Thread(this);
            t.start();
        }


        public void run() {

            try {
                file = new FileWriter("C://Users//Vagos//Desktop//file.txt");

            try
            {
                instream = new BufferedReader (new InputStreamReader (socket.getInputStream()));
                outstream = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));


                scanner = new Scanner(new File("file.txt"));


                //strin = instream.readLine();

                if ((instream.readLine()).equals("Insert")){ //following the protocol


     writeTxt(); <--heare
            }else if((instream.readLine()).equals("Read") ){
                readTxt();
            }
                instream.close();
                outstream.close();
                file.close();
                socket.close();

          System.out.println("Waiting for client message...");
        } catch (IOException e) {
            e.printStackTrace();
        }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

    private synchronized void writeTxt() throws IOException{

        do{

        number = instream.read(); <--heare 
        file.write(new Integer(number).toString());
        file.write("   ");
        file.flush();
        }while(number!=100); //bye = terminate the conversation
    }

    private void readTxt() throws IOException{

         while(scanner.hasNextInt()){
               outstream.write(scanner.nextInt());
               outstream.flush();
            }
    }




}
4

0 回答 0