• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

new line break in random access file

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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){}
}

}

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
shield kumar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"))".
 
What does a metric clock look like? I bet it is nothing like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic