aspose file tools*
The moose likes Java in General and the fly likes new line break in random access file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "new line break in random access file" Watch "new line break in random access file" New topic
Author

new line break in random access file

shield kumar
Greenhorn

Joined: Feb 05, 2009
Posts: 28
my intention is to read name ,age and address from 3 different files and write this as a single record to a output file.Don't know how can i give a newline break between two records.I tried but not getting proper result

//This program will read name,age and address from name.txt,age.txt,address.txt respectively
//and write them in finals.txt

import java.io.*;

class RandAccess
{
public static void main(String[] args)
{
try
{
File file = new File("name.txt");
RandomAccessFile raf_name = new RandomAccessFile(file, "r");

File file1 = new File("age.txt");
RandomAccessFile raf_age = new RandomAccessFile(file1, "r");

File file2 = new File("address.txt");
RandomAccessFile raf_add = new RandomAccessFile(file2, "r");

File file3 = new File("finals.txt");
RandomAccessFile raf_finals = new RandomAccessFile(file3, "rw");

String read_str; //will be used to store read string from various files
char c;


while ((read_str = raf_name.readLine()) != null)
// while (raf_name.length())
{

raf_finals.writeChars("name--");
raf_finals.writeBytes(read_str);

raf_finals.write(0x0A);
raf_finals.writeBytes("\n");

read_str = raf_age.readLine();
raf_finals.writeChars("age--");
raf_finals.writeChars(read_str); //write age

raf_finals.writeChars("address--");

while ((c = raf_add.readChar()) != ';') //read chracter untill ";" is found
{
raf_finals.writeChar(c); //write address
}


raf_finals.writeBytes("\n");
//pr.println("\n"); //used to give a line break between different records
//pr.println("<<<<>>>>>"); //marks the end of record
//raf_finals.seek(file.length());

}

raf_name.close();
raf_age.close();
raf_add.close();
raf_finals.close();

}


catch (Exception e){}
}

}

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
In the future, please UseCodeTags when posting code of any length. It's unnecessarily hard to read as it is.

If all these are text files, then you shouldn't use RandomAccessFile. That's what Readers and Writers are for. See e35 and e37 at http://www.exampledepot.com/egs/java.io/pkg.html


Ping & DNS - my free Android networking tools app
shield kumar
Greenhorn

Joined: Feb 05, 2009
Posts: 28
Hi Ulf Dittmer

i will use code tags ..thnx for the info....
but any specific reason for not using random access file for text file......

But in this case can you please let me know how to insert a line break between two records
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
From the RandomAccessFile javadocs: "Instances of this class support both reading and writing to a random access file."

Your code does neither need random access (it uses sequential access), not does need read/write access (it does read-only and write-only).

As to newlines, you can either wrap your Writer in a BufferedWriter and use its "newLine" method, or use "write(System.getProperty("line.separator"))".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: new line break in random access file