• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need Help with Eclipse

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having a problem with this program in Eclipse.


package Print;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Print {

public static void threadedPrint(File[] files) throws IOException {

PrintThread[] printThreads = new PrintThread[files.length];

for (int i=0; i<files.length; i++) {
printThreads[i] = new PrintThread(files[i]);
printThreads[i].start();
}

for (PrintThread thread : printThreads) {
try {
thread.join();
}
catch (InterruptedException ex) {
Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

private static class PrintThread extends Thread {

private File file;
private String[] data;

public PrintThread(File file) {
this.file = file;
}

public void run() {
try {
data = Print.getData(file);
for (int i=0; i<data.length; i++) {
Thread.sleep(1000);
System.out.println(data[i]);
}
}
catch (IOException ex) {
Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);
}
catch (InterruptedException ex) {
Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

private static String[] getData(File file) throws IOException {
ArrayList<String> data = new ArrayList<String>();
BufferedReader in = new BufferedReader(new FileReader(file));
while (true) {
String line = in.readLine();
if (line == null)
break;
else
data.add(line);
}
in.close();
return data.toArray(new String[0]);
}

}






The second part is this!

package Print;

import java.io.File;
import java.io.IOException;

public class PrintTest {

public static void main(String[] args) throws IOException {
File[] files = {new File("file1.txt"),
new File("file2.txt"),
new File("file3.txt"),
new File("file4.txt")};
Print.threadedPrint(files);
System.out.println("Done.");
}

}








The file texts are all under src. And I keep getting this error.


Mar 12, 2014 4:40:25 PM Print.Print$PrintThread run
SEVERE: null
java.io.FileNotFoundException: file2.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at Print.Print.getData(Print.java:59)
at Print.Print.access$0(Print.java:57)
at Print.Print$PrintThread.run(Print.java:42)
Mar 12, 2014 4:40:25 PM Print.Print$PrintThread run
SEVERE: null
java.io.FileNotFoundException: file1.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at Print.Print.getData(Print.java:59)
at Print.Print.access$0(Print.java:57)
at Print.Print$PrintThread.run(Print.java:42)
Mar 12, 2014 4:40:25 PM Print.Print$PrintThread run
SEVERE: null
java.io.FileNotFoundException: file3.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at Print.Print.getData(Print.java:59)
at Print.Print.access$0(Print.java:57)
at Print.Print$PrintThread.run(Print.java:42)
Mar 12, 2014 4:40:25 PM Print.Print$PrintThread run
SEVERE: null
java.io.FileNotFoundException: file4.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at Print.Print.getData(Print.java:59)
at Print.Print.access$0(Print.java:57)
at Print.Print$PrintThread.run(Print.java:42)
Done.





What am I doing wrong?

Please help.


Matt
 
Ranch Hand
Posts: 116
2
Eclipse IDE PHP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Matt.

First, please put any code samples within [ code ] tags so the code is formatted and easier to read.

Have you tried using fully qualified path names for the files? I.e. 'C:\your\path\here\file1.txt'
 
Matthew Filippone
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where do I put that?
 
Matthew Filippone
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And do I keep those files in the src?
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE PHP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be able to add the path to your file name when you create a new File object:

 
Matthew Filippone
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gotcha
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic