Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File saving

Posted on 2004-09-09
14
Medium Priority
?
202 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 80 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

670 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