Streams and Files

Writing out to files and reading from the files

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 asdbabil  Icon User is offline

  • D.I.C Head

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

Streams and Files

Posted 02 March 2010 - 07:17 PM

Hi guys
The following code creates one file at a time by reading jokes from an array. But I'm trying to have it read in from a text file 5-10 jokes, and then create 3 files at the same time (dat, byte and object type). Help me please.

import java.util.*;
import java.io.*;

public class Joking
{

static final String dataFile = "OldByte.obj ";

static final String[] jokes = {"Chuck Norris could use anything in java.util.* to kill you, including the javadocs.", "If you get a ChuckNorrisException you’ll probably die.", "Chuck Norris doesn't travel at the speed of light, light travels at the speed of Chuck Norris!", "Chuck Norris can make a class that is both abstract and final.", "Every single line code of Chuck Norris runs in real time. Even in a multi threading application.", "Chuck Norris doesn’t write code. He stares at a computer screen until he gets the program he wants.", "Chuck Norris methods don’t catch exceptions because no one has the guts to throw any at them."};

    public static void main(String[] args) throws IOException
    	{


	/*
    	Scanner scanner = new Scanner(new FileRead("jokes.txt"));
    	
    	
    	            while (scanner.hasNextLine())
            {
            	String line = scanner.nextLine();
				System.out.println(line);


            }
            */

            DataOutputStream outDat = null;
            //DataOutputStream outTxt = null;
            //DataOutputStream outObj = null;

            try
            	{

    	  outDat = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));
    	 //outTxt = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));
    	 //outObj = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));

    	for (int i = 0; i < jokes.length; i++)
    		{
    			  outDat.writeUTF(jokes[i]);
    			//outTxt.writeUTF(jokes[i]);
    			//outObj.writeUTF(jokes[i]);
    		}
            }

    	finally
    		{
    			outDat.close();
    		}

    }

}



This post has been edited by asdbabil: 03 March 2010 - 06:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Streams and Files

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Streams and Files

Posted 02 March 2010 - 07:19 PM

What exactly is the problem you are running into in accomplishig this task?
Was This Post Helpful? 0
  • +
  • -

#3 asdbabil  Icon User is offline

  • D.I.C Head

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

Re: Streams and Files

Posted 02 March 2010 - 07:23 PM

View Postmacosxnerd101, on 02 March 2010 - 08:19 PM, said:

What exactly is the problem you are running into in accomplishig this task?


It works but it creates only one single file at a time, and it reads from the array that I have.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Streams and Files

Posted 02 March 2010 - 07:27 PM

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);
}


Was This Post Helpful? 1
  • +
  • -

#5 asdbabil  Icon User is offline

  • D.I.C Head

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

Re: Streams and Files

Posted 02 March 2010 - 07:43 PM

View Postmacosxnerd101, on 02 March 2010 - 08: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);
}



Ok I see all three files, but shouldn't the content of the files be in binary code?

This post has been edited by asdbabil: 02 March 2010 - 07:51 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Streams and Files

Posted 02 March 2010 - 07:51 PM

View Postasdbabil, on 02 March 2010 - 08:43 PM, said:

Ok I see all three files, but shouldn't the content of the files in binary code?

ALL files content are in binary code. What do you mean ?
Was This Post Helpful? 0
  • +
  • -

#7 asdbabil  Icon User is offline

  • D.I.C Head

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

Re: Streams and Files

Posted 02 March 2010 - 07:56 PM

View Postpbl, on 02 March 2010 - 08:51 PM, said:

View Postasdbabil, on 02 March 2010 - 08:43 PM, said:

Ok I see all three files, but shouldn't the content of the files in binary code?

ALL files content are in binary code. What do you mean ?


Maybe I'm not saying the right coding.

This post has been edited by asdbabil: 03 March 2010 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

#8 asdbabil  Icon User is offline

  • D.I.C Head

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

Re: Streams and Files

Posted 02 March 2010 - 08:04 PM

Ok never mind that. How can i make it read the jokes from a text file instead of having an array?

This post has been edited by asdbabil: 02 March 2010 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Streams and Files

Posted 02 March 2010 - 08:07 PM

Take a look at using the java.util.Scanner class.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Streams and Files

Posted 02 March 2010 - 08:18 PM

View Postasdbabil, on 02 March 2010 - 09:04 PM, said:

Ok never mind that. How can i make it read the jokes from a text file instead of having an array?

int i = 0;
String[] joke = new Strong[100];
Scanner scan = new Scanner(new File("joke.txt"));
while(scan.hasNext()) {
    joke[i] = scan.nextLine();
    i++;
}


Was This Post Helpful? 1
  • +
  • -

#11 asdbabil  Icon User is offline

  • D.I.C Head

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

Re: Streams and Files

Posted 02 March 2010 - 08:40 PM

it compiles, it runs, but it fails to create the files. (the "jokes.txt" file has the jokes and it is with the java file)!! Here's my final code
import java.util.*;
import java.io.*;



public class Joking
{

static final String dataFileOne = "jokesDat.dat";
static final String dataFileTwo = "jokesChar.txt";
static final String dataFileThree = "jokesObj.obj";


    public static void main(String[] args) throws IOException{


            
            int i = 0;
String[] joke = new String[100];
Scanner scan = new Scanner(new File("jokes.txt"));
while(scan.hasNext()) {
    joke[i] = scan.nextLine();
    i++;
}


            DataOutputStream outDat = null;
            DataOutputStream outTxt = null;
            DataOutputStream outObj = null;

            try{


    	 outDat = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFileOne)));
    	 outTxt = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFileTwo)));
    	 outObj = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFileThree)));

    	for ( i = 0; i < joke.length; i++){
    			outDat.writeUTF(joke[i]);
    			outTxt.writeUTF(joke[i]);
    			outObj.writeUTF(joke[i]);
    		}
            }catch (Exception e){
            	System.out.println("Creation failed!");
            	System.exit(0);
            }

    	finally{
    			outDat.close();
    			outTxt.close();
    			outObj.close();
    		}


    	
    }

}



This post has been edited by asdbabil: 02 March 2010 - 08:50 PM

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Streams and Files

Posted 02 March 2010 - 09:15 PM

first, add a println to make sure you read correctly
String[] joke = new String[100]; 
Scanner scan = new Scanner(new File("jokes.txt")); 
while(scan.hasNext()) { 
    joke[i] = scan.nextLine(); 
    System.out.println("Joke #" + i + " is " + joke[i]);
    i++; 
} 


Was This Post Helpful? 1
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Streams and Files

Posted 02 March 2010 - 09:17 PM

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
Was This Post Helpful? 1
  • +
  • -

#14 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 - 11:24 AM

I appreciate the help but this assignment is just part A, and the whole is assignment is just very hard for me. The instructor doesn't explain very well and his assignments are very advanced.

This post has been edited by asdbabil: 03 March 2010 - 01:19 PM

Was This Post Helpful? 0
  • +
  • -

#15 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 - 06:27 PM

View Postpbl, on 02 March 2010 - 10:15 PM, said:

first, add a println to make sure you read correctly
String[] joke = new String[100]; 
Scanner scan = new Scanner(new File("jokes.txt")); 
while(scan.hasNext()) { 
    joke[i] = scan.nextLine(); 
    System.out.println("Joke #" + i + " is " + joke[i]);
    i++; 
} 



I did add a println and I got null for all jokes! Why is that
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2