Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Java error in I/O Code


  • Please log in to reply

#1
chancit2003

chancit2003

    Member

  • Member
  • PipPip
  • 15 posts
Here is the code I was writing to demonstrate pipes for I/O....

import java.io.*;

class PipeApp
{
public static void main(String[] args)
{
PipeApp pipeApp = new PipeApp();
try
{
FileInputStream AFileIn = new FileInputStream("input.txt");
InputStream BInPipe = pipeApp.changeToB(AFileIn);
InputStream CInPipe = pipeApp.changeToC(BInPipe);
System.out.println();
System.out.println("Here is the results:");
System.out.println();
BufferedReader inputStream = new BufferedReader(new InputStreamReader(CInPipe));
String str = inputStream.readLine();
while (str != null)
{
System.out.println(str);
str = inputStream.readLine();
}
inputStream.close();
}
catch (InterruptedException e)
{
System.out.println(e.toString());
}
}

public InputStream changeToB(InputStream inputStream)
{
try
{
BufferedReader AFileIn = new BufferedReader(new InputStreamReader(inputStream));
PipedOutputStream pipeOut = new PipedOutputStream();
PipedInputStream pipeIn = new PipedInputStream(pipeOut);
PrintStream printStream = new PrintStream(pipeOut);
Thread bThread = new Thread(AFileIn, printStream);
bThread.start();
return pipeIn;
}
catch (InterruptedException e)
{
System.out.println(e.toString());
}
return null;
}
public InputStream changeToC(InputStream inputStream)
{
try
{
BufferedReader BFileIn = new BufferedReader(new InputStreamReader(inputStream));
PipedOutputStream pipeOut2 = new PipedOutputStream();
PipedInputStream pipeIn2 = new PipedInputStream(pipeOut2);
PrintStream printStream2 = new PrintStream(pipeOut2);
Thread cThread = new Thread(BFileIn, printStream2);
cThread.start();
return pipeIn2;
}
catch (InterruptedException e)
{
System.out.println(e.toString());
}
return null;
}

}

I keep getting an error on the two lines that are:
Thread bThread = new Thread(AfileIn, printStream2);
and
Thread cThread = new Thread(BfileIn, printStream2);

when I compile it gives me an error about the "new" it says:
cannot resolve symbol...

I may have messed up elsewhere but I remember being told that the first error is usually the error and those are the only two errors I get.

Can anyone assist me,

Thanks

Kim
  • 0

Advertisements


#2
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
hi chancit2003,

the first message from the compiler is

PipeApp.java:42: cannot resolve symbol
symbol : constructor Thread (java.io.BufferedReader,java.io.PrintStream)
location: class java.lang.Thread
Thread bThread = new Thread(AFileIn, printStream);


it has a problem with 'constructor Thread (java.io.BufferedReader,java.io.PrintStream)'. there is no Thread() constructor that takes java.io.BufferedReader and java.io.PrintStream as arguments.
see http://java.sun.com/...ang/Thread.html to view Thread() constructors.

notice that the compiler has identified the line number 'PipeApp.java:42' and source code file.

also notice that the offending line is shown 'Thread bThread = new Thread(AFileIn, printStream);'.

it is pointing to 'new' since it cannot create a new Thread() object.

it appears that these lines are unnecessary. why are you using threads here?

if the Thread lines are removed, the remaining task is to include try/catch blocks for the io exceptions that the compiler will kindly point out.

bdlt
  • 0

#3
ejay563

ejay563

    Member

  • Member
  • PipPip
  • 92 posts
when you use the java.io.* package don't you have to have throws IOException in the method in which you are using it? Ie:

import java.io.*

public class example
{
public static void main(String [] args) throws IOException
{
All you code goes here
}
}

Edited by ejay563, 31 January 2006 - 12:00 AM.

  • 0

#4
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts

when you use the java.io.* package don't you have to have throws IOException in the method in which you are using it?

Nope. The code will not compile unless all exceptions that need handling (i.e. not run-time exceptions) are handled. You can handle them by either having the method throw them, or by catching them. If you have the method throw them, then as soon as an exception is thrown, the program will end. By using a try-catch statement, it will just exit the try clause and excecute the code in the catch clause.

Hope this helps!
  • 0

#5
coyne20

coyne20

    Member

  • Member
  • PipPip
  • 35 posts
Im sure that Java has only 4 Thread Constructor types:

Thread t = new Thread();
Thread t = new Thread(Runnable r);
Thread t = new Thread(Runnable r, String s);
Thread t = new Thread(String s);
  • 0

#6
coyne20

coyne20

    Member

  • Member
  • PipPip
  • 35 posts
Another thing is that you havent defined a run() method used by the threads
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP