为什么 readLine() 导致 InputStreamReader 出现未定义错误?
Why is readLine() resulting in undefined error with InputStreamReader?
我正在 Java 创建一个简单的 IRC 客户端。我 运行 在尝试使用包含在 BufferedReader 中的 InputStreamReader 从服务器读取数据流时遇到了问题。我发现的示例都使用 readLine() 方法来捕获数据。但是,当我使用这种方法时,我收到以下错误:
readLine() is undefined for the type InputStreamReader
错误首先出现在我的 while
循环中。这是代码:
public class IRCClient {
//Initialize the input and output streams
private Socket socket = null;
private InputStreamReader reader = null; //What we receive from the host
private OutputStreamWriter writer = null; //What we send to the host
//Client constructor
public IRCClient(String address, int port, String nick, String login, String channel) {
//Establish a connection to the host
try
{
socket = new Socket(address, port);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}
//Login to the host
try
{
writer.write("NICK " + nick + "\r\n");
writer.write("USER " + login + "\r\n");
writer.flush();
}
catch(IOException i)
{
System.out.println(i);
}
//Read lines from the server to make sure we are connected
String line = null;
try
{
while ((line = reader.readLine()) != null) {
if (line.indexOf("004") >= 0) {
// We are logged in
break;
} else if (line.indexOf("433") >= 0) {
System.out.println("Nickname is already in use");
return;
}
}
} catch (IOException i) {
System.out.println(i);
}
// Join the specified channel
writer.write("JOIN " + channel + "\r\n");
writer.flush();
// Keep reading lines from the host.
while ((line = reader.read()) != null) {
if (line.toLowerCase().startsWith("PING ")) {
//Respond with PONG tro avoid being disconnected
writer.write("PONG " + line.substring(5) + "\r\n");
writer.flush();
}
else {
//Print the line received
System.out.println(line);
}
} //end while
}
//Instantiate the client object
public static void main(String[] args){
//Connection details
String serverAddress = "irc.chat.twitch.tv";
int serverPort = 6667;
String nick = args[0];
String login = args[1];
String channel = args[2];
IRCClient client = new IRCClient(serverAddress, serverPort, nick, login, channel);
} //end main
} //end class
这里的问题是,变量 reader
和 writer
在 while 循环中不可见,但仅在它们声明的 try-catch-block 中可见。
如果您像这样更改代码,它至少应该可以编译(没有测试 id,因为我没有服务器实现):
//Client constructor
public IRCClient(String address, int port, String nick, String login, String channel) throws IOException {//EDITED HERE added throws declaration (either do this, or catch the exceptions)
//Client constructor
public IRCClient(String address, int port, String nick, String login, String channel) throws IOException {//EDITED HERE added throws declaration (either do this, or catch the exceptions)
//Establish a connection to the host
//EDITED HERE move the declaration here to have the variables visible
BufferedWriter writer = null;
BufferedReader reader = null;
try {
socket = new Socket(address, port);
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch (UnknownHostException u) {
System.out.println(u);
}
catch (IOException i) {
System.out.println(i);
}
//Login to the host
try {
writer.write("NICK " + nick + "\r\n");
writer.write("USER " + login + "\r\n");
writer.flush();
}
catch (IOException i) {
System.out.println(i);
}
//Read lines from the server to make sure we are connected
String line = null;
try {
while ((line = reader.readLine()) != null) {
if (line.indexOf("004") >= 0) {
// We are logged in
break;
}
else if (line.indexOf("433") >= 0) {
System.out.println("Nickname is already in use");
return;
}
}
}
catch (IOException i) {
System.out.println(i);
}
// Join the specified channel
writer.write("JOIN " + channel + "\r\n");
writer.flush();
// Keep reading lines from the host.
while ((line = reader.readLine()) != null) {//EDITED HERE replaced read() with readLine()
if (line.toLowerCase().startsWith("PING ")) {
//Respond with PONG tro avoid being disconnected
writer.write("PONG " + line.substring(5) + "\r\n");
writer.flush();
}
else {
//Print the line received
System.out.println(line);
}
} //end while
}
我用评论标记了我更改的部分//EDITED HERE
您没有初始化字段,您正在创建未使用的局部变量,并且您的字段类型错误。
改变
private InputStreamReader reader = null; //What we receive from the host
至
private BufferedReader reader;
并改变
BufferedReader reader = new BufferedReader(
至
reader = new BufferedReader(
你也会遇到与作者类似的问题。为此做类似的事情。
我正在 Java 创建一个简单的 IRC 客户端。我 运行 在尝试使用包含在 BufferedReader 中的 InputStreamReader 从服务器读取数据流时遇到了问题。我发现的示例都使用 readLine() 方法来捕获数据。但是,当我使用这种方法时,我收到以下错误:
readLine() is undefined for the type InputStreamReader
错误首先出现在我的 while
循环中。这是代码:
public class IRCClient {
//Initialize the input and output streams
private Socket socket = null;
private InputStreamReader reader = null; //What we receive from the host
private OutputStreamWriter writer = null; //What we send to the host
//Client constructor
public IRCClient(String address, int port, String nick, String login, String channel) {
//Establish a connection to the host
try
{
socket = new Socket(address, port);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}
//Login to the host
try
{
writer.write("NICK " + nick + "\r\n");
writer.write("USER " + login + "\r\n");
writer.flush();
}
catch(IOException i)
{
System.out.println(i);
}
//Read lines from the server to make sure we are connected
String line = null;
try
{
while ((line = reader.readLine()) != null) {
if (line.indexOf("004") >= 0) {
// We are logged in
break;
} else if (line.indexOf("433") >= 0) {
System.out.println("Nickname is already in use");
return;
}
}
} catch (IOException i) {
System.out.println(i);
}
// Join the specified channel
writer.write("JOIN " + channel + "\r\n");
writer.flush();
// Keep reading lines from the host.
while ((line = reader.read()) != null) {
if (line.toLowerCase().startsWith("PING ")) {
//Respond with PONG tro avoid being disconnected
writer.write("PONG " + line.substring(5) + "\r\n");
writer.flush();
}
else {
//Print the line received
System.out.println(line);
}
} //end while
}
//Instantiate the client object
public static void main(String[] args){
//Connection details
String serverAddress = "irc.chat.twitch.tv";
int serverPort = 6667;
String nick = args[0];
String login = args[1];
String channel = args[2];
IRCClient client = new IRCClient(serverAddress, serverPort, nick, login, channel);
} //end main
} //end class
这里的问题是,变量 reader
和 writer
在 while 循环中不可见,但仅在它们声明的 try-catch-block 中可见。
如果您像这样更改代码,它至少应该可以编译(没有测试 id,因为我没有服务器实现):
//Client constructor
public IRCClient(String address, int port, String nick, String login, String channel) throws IOException {//EDITED HERE added throws declaration (either do this, or catch the exceptions)
//Client constructor
public IRCClient(String address, int port, String nick, String login, String channel) throws IOException {//EDITED HERE added throws declaration (either do this, or catch the exceptions)
//Establish a connection to the host
//EDITED HERE move the declaration here to have the variables visible
BufferedWriter writer = null;
BufferedReader reader = null;
try {
socket = new Socket(address, port);
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch (UnknownHostException u) {
System.out.println(u);
}
catch (IOException i) {
System.out.println(i);
}
//Login to the host
try {
writer.write("NICK " + nick + "\r\n");
writer.write("USER " + login + "\r\n");
writer.flush();
}
catch (IOException i) {
System.out.println(i);
}
//Read lines from the server to make sure we are connected
String line = null;
try {
while ((line = reader.readLine()) != null) {
if (line.indexOf("004") >= 0) {
// We are logged in
break;
}
else if (line.indexOf("433") >= 0) {
System.out.println("Nickname is already in use");
return;
}
}
}
catch (IOException i) {
System.out.println(i);
}
// Join the specified channel
writer.write("JOIN " + channel + "\r\n");
writer.flush();
// Keep reading lines from the host.
while ((line = reader.readLine()) != null) {//EDITED HERE replaced read() with readLine()
if (line.toLowerCase().startsWith("PING ")) {
//Respond with PONG tro avoid being disconnected
writer.write("PONG " + line.substring(5) + "\r\n");
writer.flush();
}
else {
//Print the line received
System.out.println(line);
}
} //end while
}
我用评论标记了我更改的部分//EDITED HERE
您没有初始化字段,您正在创建未使用的局部变量,并且您的字段类型错误。
改变
private InputStreamReader reader = null; //What we receive from the host
至
private BufferedReader reader;
并改变
BufferedReader reader = new BufferedReader(
至
reader = new BufferedReader(
你也会遇到与作者类似的问题。为此做类似的事情。