Results 1 to 2 of 2

Thread: Write to file

  1. #1
    esadeghi is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default Write to file

    i use below code to write a character to a file, the old contents cleared but nothing is written :
    File edgesFile = new File(netFile);
    FileOutputStream inFile = null;
    try
    {
    inFile = new FileOutputStream(edgesFile);
    }
    catch(FileNotFoundException e)
    {
    e.printStackTrace(System.err);
    System.exit(1);
    }
    FileChannel inChannel = inFile.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(48);
    buf.putChar('q');
    try
    {
    inChannel.write(buf);
    inChannel.force(true);
    inChannel.close();
    }
    catch(IOException e)
    {
    e.printStackTrace(System.err);
    }
    and another thing how can i write a string array to a file that any element be in one line?

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    It depends on how you will save them in a file....

    You may try to use Formatter for saving files in format, and also the FileWriter....

    Those classes are safe to implement.
    Java Code:
    void WriteFile throws Exception(){
         FileWriter fw = new FileWriter(netFile);
         fw.append('q');
         fw.close();
    }
    Java Code:
    void WriteFile throws Exception(){
         Formatter write = new Formatter(netFile);
         write.format("%c",'q');
         write.close();
    }
    try to test it,
    Last edited by sukatoa; 05-21-2008 at 07:09 PM. Reason: Scanner changed to Formatter....
    freedom exists in the world of ideas

Similar Threads

  1. how to write onto a file
    By mirage_87 in forum New To Java
    Replies: 6
    Last Post: 09-08-2009, 03:54 PM
  2. how do i write to a text file from an arraylist?
    By otoro_java in forum New To Java
    Replies: 3
    Last Post: 01-30-2008, 06:53 AM
  3. File Write Error
    By vikain in forum Advanced Java
    Replies: 5
    Last Post: 01-02-2008, 04:38 AM
  4. Write unicode into file
    By vata2999 in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 03:04 PM
  5. Help with write file in java
    By mathias in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •