Solved

File saving

Posted on 2004-09-09
14
196 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now