Solved

File saving

Posted on 2004-09-09
14
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

734 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