Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

File saving

I have some File objects. Is it possible to save these objects into a File again? If so, which method of it?
0
jtcy
Asked:
jtcy
  • 6
  • 4
  • 2
  • +1
1 Solution
 
CEHJCommented:
You can't save File objects. But please clarify
0
 
jtcyAuthor Commented:
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
 
girionisCommented:
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.

 
CEHJCommented:
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
 
jtcyAuthor Commented:
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
 
jtcyAuthor Commented:
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
 
girionisCommented:
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
 
RuadRauFlessaCommented:
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
 
jtcyAuthor Commented:
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
 
RuadRauFlessaCommented:
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
 
jtcyAuthor Commented:
it says missing term at the + operator.
0
 
RuadRauFlessaCommented:
oops sorry that should be a closing brace like the ammendment below

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


Sorry again slip of a key :(
0
 
jtcyAuthor Commented:
thanks
0
 
RuadRauFlessaCommented:
No thank you this gave me a refressing break from my daily work. Also glad to be of service ;-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now