Solved

File saving

Posted on 2004-09-09
14
199 Views
Last Modified: 2010-03-31
I have some File objects. Is it possible to save these objects into a File again? If so, which method of it?
0
Comment
Question by:jtcy
  • 6
  • 4
  • 2
  • +1
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12015624
You can't save File objects. But please clarify
0
 

Author Comment

by:jtcy
ID: 12015666
See...I am building a chat program. Then it needs to save transcript and shut down the server. Alright, that is fine, no problem, but a client should be able to load the saved transcript. The thing is, I have the server saving the previous transcript as a FILE. Thus, when the client wants to load a transcript, he would open a load file dialog box to choose the file. I was thinking, to save all those files into one file and when the client wants to load transcripts, just go internally to that file and get those files within it for the client to choose. So that i wont take the risk of the client choosing a wrong format file himself. um......u know what i mean?
0
 
LVL 35

Expert Comment

by:girionis
ID: 12015683
Have a look here: http://javaalmanac.com/egs/java.io/CreateTempFile.html for saving files.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12015701
Well, you can save a file, but not a File object. But i won't confuse things further ..;-)

You can load and save files no problem. What's the difficulty?
0
 

Author Comment

by:jtcy
ID: 12015743
See, if i could make things internally, then i wont have any risk of having the client choosing wrong format file in the dialog box.

For example, if I could store all transcript FILEs in one big File, then, when the client clicks on "load transcript", i could just internally go to that big File and get all those files within it and display them in a JList and the user can choose from there.

Apparently, this is not the case. The client clicks on the load transcript button and a dialog box would come out and let him choose a transcript in his own directory. So, there may be risk of having the client choosing a not-correct transcript.

hm~~~~~
0
 

Author Comment

by:jtcy
ID: 12015830
Nvm, i think i will save the transcripts as vector and then save all those vectors into one file. So, can a File object store vectors?
0
 
LVL 35

Expert Comment

by:girionis
ID: 12015891
Just serializethe vector and you should be fine. Just make sure the objects you stoer in the vector are serializable:

http://javaalmanac.com/egs/java.io/SerializeObj.html
0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12016377
No add your File objects into a vector and then write that to a file. Later on you can just read them back.

Take a look at the ObjectOutputStream, ObjectInputStream, FileInputStream and ileOutputStream classes.

Another alternative would be to create a kind of playlist file such as the format below

#description1
c:\filepath\file1
#description2
c:\filepath\file1

Then you could just ennumerate them and instantiate numerous file object from them.


Or you could go so far as to write all of the files into a single file using the java.util.zip package. Then you would also save some space and have a fast and easy access to the content thereof.
0
 

Author Comment

by:jtcy
ID: 12016629
Okay.

Um.....can someone check why the following is not working?

Vector tempor = new Vector();
            
            
           try
            {
                  FileReader reader = new FileReader("transcriptNames");
               BufferedReader in = new BufferedReader(reader);
              
                  String inputLine = null;
           
                 while(in.readLine() != null)
                    {
                        inputLine = in.readLine();
                        tempor.add(inputLine);
                    }
            }
           
           catch (Exception e)
            {
                  System.out.println("Exception: " + e.toString());
            }
           
           System.out.println((String)tempor.get(0));


It prints out as null. The file has one word in it though.


0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12016767
Do this and it will work.


Vector tempor = new Vector();
         
         
          try
           {
                FileReader reader = new FileReader("transcriptNames");
              BufferedReader in = new BufferedReader(reader);
             
                String inputLine = null;
         
               while((inputLine = in.readLine()+ != null)
                  {                    
                     tempor.add(inputLine);
                  }
           }
         
          catch (Exception e)
           {
                System.out.println("Exception: " + e.toString());
           }
         
          System.out.println((String)tempor.get(0));

What you did was read once in the while statement which would give you a valid condition cuz you have one entry in your file. Then you read again to assign a value to inputLine. By doing that first read you effectively read past the values you have in the file so the in.readLine() method returned a null and you added that null reference to the Vector which is what you got back when you did the System.out.println();
0
 

Author Comment

by:jtcy
ID: 12016804
it says missing term at the + operator.
0
 
LVL 3

Accepted Solution

by:
RuadRauFlessa earned 20 total points
ID: 12016918
oops sorry that should be a closing brace like the ammendment below

while((inputLine = in.readLine()) != null)


Sorry again slip of a key :(
0
 

Author Comment

by:jtcy
ID: 12017035
thanks
0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12017072
No thank you this gave me a refressing break from my daily work. Also glad to be of service ;-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Opening PDF on button click and fill new document 2 47
How do I remove an object from a 3 40
CSV file parsing thru Java 13 34
Java basic valueOf question 1 29
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question