Streams and Files

Writing out to files and reading from the files

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1141 Views - Last Post: 04 March 2010 - 11:06 AM Rate Topic: -----

#16 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Streams and Files

Posted 03 March 2010 - 08:30 PM

View Postmacosxnerd101, on 02 March 2010 - 10:17 PM, said:

View Postmacosxnerd101, on 02 March 2010 - 10:27 PM, said:

Just create three File objects and wrap your three OutputStreams, one around each File.
File one = new File("one.dat");
File two = new File("two");
File three = new File("three");

try{
   one.createNewFile();
   two.createNewFile();
   three.createNewFile();
}
catch(Exception e){
   System.out.println("Creation failed");
   System.exit(0);
}



You should probably create File objects first and use the File createNewFile() method to create the Files on the hard drive, as I showed in one of my earlier posts. Note that the File constructor accepts a String denoting the path of the File (or simply the name if it is local to the folder).

For more information on the File class, check out the API:
http://java.sun.com/...va/io/File.html


You mean I need to replace
outDat.writeUTF(joke[i]);
outTxt.writeUTF(joke[i]);
outObj.writeUTF(joke[i]);



with
one.createNewFile();
two.createNewFile();
three.createNewFile();


Was This Post Helpful? 0
  • +
  • -

#17 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10667
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: Streams and Files

Posted 03 March 2010 - 08:51 PM

What I'm saying is that you should create your Files before writing to them. So if you have a File object named one:
File one = new File("one");
DataOutputStream outDat = null;

try{
    one.createNewFile(); //create File on your hard drive
    outDat = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(one))); //and open a stream to write to it
}
catch(Exception e){
    System.exit(0);
}

//Now write to the File


Was This Post Helpful? 1
  • +
  • -

#18 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2780
  • View blog
  • Posts: 11,765
  • Joined: 20-September 08

Re: Streams and Files

Posted 04 March 2010 - 02:13 AM

Quote

(dat, byte and object type).


What does that mean? What is your assignment (verbatim please)?
Was This Post Helpful? 0
  • +
  • -

#19 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Streams and Files

Posted 04 March 2010 - 11:06 AM

View Postg00se, on 04 March 2010 - 03:13 AM, said:

Quote

(dat, byte and object type).


What does that mean? What is your assignment (verbatim please)?


All i want it to do is read the jokes from a text file and then write them out to three different files with three different extensions (fileName1.obj, fileName2.dat, fileName3.txt), that's part A.

Part B,
Create a class called FileWatcher.
• This class can be given several filenames that may or may not exist corresponding to .dat, .txt and .obj files.
• Include all the filenames which corresponds to the associated file type from Assignment 2A above.
• For example FileWatcherObj should include all filenames created with the suffix .obj from Assignment 2A.

A thread of execution should be started for each filename.
Each thread will periodically check for the existence of its file. Enter in “exit” when all the file names you are watching have been entered. Be sure to enter a file that does not exist.

If the file appears, the thread will write a message indicating the file that was found to the console.
• Then, it will randomly display a joke in its separate JFrame thread properly titled for the classification.
• Each joke should display for two second for every 5 words in the joke. Jokes should be only less than 3 sentences.

Also create another thread that checks to see if all the jokes are displayed at least twice.
When all have been displayed, terminate the execution.

Hint: Create static ThreadGroup object in a class (ReadFile) so you can refer to the thread group object with the class name for all the different treads.

static ThreadGroup fileWatchers=new ThreadGroup("File Watchers");

Put all the FileWatcher threads in a thread group so you can terminate the group of threads as a whole by calling their interrupt() method that is a condition in the run() methods loop.

ReadFile.fileWatchers.interrupt();
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2