By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,594 Members | 3,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,594 IT Pros & Developers. It's quick & easy.

Writing part of a line to a file

P: 31
RandomAccessFile readName = null;
RandomAccessFile nameWriter= null;
List<String> Names = new ArrayList<String>();
char[] pan= new char[19];
char[] cardName= new char[23];
try{
readName= new RandomAccessFile(persoNames + "/perso.txt","rw");
nameWriter= new RandomAccessFile("C:/perso.txt","rw");
for (String line = readName.readLine(); line != null;
line = readName.readLine()) {
if (line != null){
//readName.
line.getChars(0,19,pan,0);
line.getChars(19,42,cardName,0);
nameWriter.writeChars(pan.toString());

I tried to use the above code to write the info in the character array to a file. I get the correct thing first time then later I get garbage.

Could you direct me to any web resources to help me correct the code.
Sep 24 '07 #1
Share this Question
Share on Google+
10 Replies


10K+
P: 13,264
RandomAccessFile readName = null;
RandomAccessFile nameWriter= null;
List<String> Names = new ArrayList<String>();
char[] pan= new char[19];
char[] cardName= new char[23];
try{
readName= new RandomAccessFile(persoNames + "/perso.txt","rw");
nameWriter= new RandomAccessFile("C:/perso.txt","rw");
for (String line = readName.readLine(); line != null;
line = readName.readLine()) {
if (line != null){
//readName.
line.getChars(0,19,pan,0);
line.getChars(19,42,cardName,0);
nameWriter.writeChars(pan.toString());

I tried to use the above code to write the info in the character array to a file. I get the correct thing first time then later I get garbage.

Could you direct me to any web resources to help me correct the code.
You want to insert text at a particular position in a file or append it to the end?
Why are you using two RandomAccessFile objects?
Sep 24 '07 #2

P: 31
You want to insert text at a particular position in a file or append it to the end?
Why are you using two RandomAccessFile objects?

/*
* NamesCount.java
*
* Created on 13 September 2007, 14:06
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package namescount;
import java.io.*;
import java.util.*;
/**
*
* @author oshonowo
*/
public class NamesCount {
public static void main(String[] args) throws Exception{
File Dir = new File("//10.10.0.127/c$/Documents and Settings/oshonowo/My Documents/Chams to be burnt/20070921");
List<String> allNames = new ArrayList<String>();
if( Dir.exists() && Dir.isDirectory()){
String[] location = Dir.list();
for(int i=0; i <location.length; i++){
File dirs= new File(Dir + "/" + location[i]);
//SumOfCards+=NumberofLines(dirs);
allNames.add(PrintNames(dirs));
}
}
System.out.println(" Names are "+ allNames);
//System.out.println("Sum of cards is "+ SumOfCards);

}

static String PrintNames(File persoNames) throws Exception{
RandomAccessFile readName = null;
RandomAccessFile nameWriter= null;
List<String> Names = new ArrayList<String>();
char[] pan= new char[19];
char[] cardName= new char[23];
try{
readName= new RandomAccessFile(persoNames + "/perso.txt","rw");
nameWriter= new RandomAccessFile("C:/perso.txt","rw");
for (String line = readName.readLine(); line != null;
line = readName.readLine()) {
if (line != null){
//readName.
line.getChars(0,19,pan,0);
line.getChars(19,42,cardName,0);
nameWriter.writeChars(pan.toString());
//for (int i=0; i< pan.length; i++){ nameWriter.writeChars(pan.);}
//for (int i=0; i< cardName.length; i++){nameWriter.writeBytes(cardName[i]);}
//System.out.println(pan + " " + cardName);
Names.add(line);
nameWriter.writeBytes(System.getProperty("line.sep arator"));
}
}
//Names.add("%n");
if (Names.isEmpty()!= true){
Collections.sort(Names);
//System.out.println(Names);
nameWriter.writeBytes(Names.toString());
}
}catch (Exception e) {
System.err.format("Could not read %s: %s%n", Names, e);
System.exit(1);
} finally {
if (readName != null) {
try {
readName.close();
} catch (IOException e) {}
}
if (nameWriter != null) {
try {
nameWriter.close();
} catch (IOException e) {}
}
}
return Names.toString();

}

/* static int NumberofLines(File file) throws IOException{
BufferedReader reader = null;
int lineCount = 0;
try {
reader = new BufferedReader(new FileReader(file + "/perso.txt"));
for (String line = reader.readLine(); line != null;
line = reader.readLine()) {
lineCount++;
}
} catch (IOException e) {
System.err.format("Could not read %s: %s%n", file, e);
System.exit(1);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {}
}
}
System.out.println("Number of Lines are " + lineCount);
return lineCount;
}*/
}

I am trying to write a program that will read the contents of a directory on another system and prints the relevant parts to a file.

I just need to know where I can find info on how to read part of a string representing a line of info., extract the relevant parts and write them to a different file.

I have tried using the API on the sun website but it is a bit hard for a beginner like me.

This is office work. Pls help.
Sep 25 '07 #3

dmjpro
100+
P: 2,476
Expand|Select|Wrap|Line Numbers
  1. RandomAccessFile readName = null;
  2.         RandomAccessFile nameWriter= null;
  3.         List<String> Names = new ArrayList<String>();
  4.         char[] pan= new char[19];
  5.         char[] cardName= new char[23];
  6.         try{
  7.             readName= new RandomAccessFile(persoNames + "/perso.txt","rw");
  8.             nameWriter= new RandomAccessFile("C:/perso.txt","rw");
  9.             for (String line = readName.readLine(); line != null;
  10.                     line = readName.readLine()) {
  11.                 if (line != null){
  12.                     //readName.
  13.                     line.getChars(0,19,pan,0);
  14.                     line.getChars(19,42,cardName,0);
  15.                     nameWriter.writeChars(pan.toString());
  16.  
I tried to use the above code to write the info in the character array to a file. I get the correct thing first time then later I get garbage.

Could you direct me to any web resources to help me correct the code.
Use Code Tags while you do Post!
Now try to extract the line.length(), whether it is 42 bytes long or something else :-)

Kind regards,
Dmjpro.
Sep 25 '07 #4

P: 31
Use Code Tags while you do Post!
Now try to extract the line.length(), whether it is 42 bytes long or something else :-)

Kind regards,
Dmjpro.


It is something else. It changes for every line.
Sep 25 '07 #5

dmjpro
100+
P: 2,476
It is something else. It changes for every line.
Now explain me two lines below. :-)

Expand|Select|Wrap|Line Numbers
  1. line.getChars(0,19,pan,0);
  2. //Here you sure that first 19 bytes will be Pan
  3. line.getChars(19,42,cardName,0);
  4. //Here you same too.....
  5.  
I assumed that first 19 bytes will be Pan and then rest bytes will be Card.

Now try with this modified line..
Expand|Select|Wrap|Line Numbers
  1. line.getChars(19,line.length(),cardName,0);
  2.  
Best of Luck with your Try!

Kind regards,
Dmjpro.
Sep 25 '07 #6

P: 31
[quote=dmjpro]Now explain me two lines below. :-)

Expand|Select|Wrap|Line Numbers
  1. line.getChars(0,19,pan,0);
  2. //Here you sure that first 19 bytes will be Pan
  3. line.getChars(19,42,cardName,0);
  4. //Here you same too.....
  5.  
I assumed that first 19 bytes will be Pan and then rest bytes will be Card.

Now try with this modified line..
Expand|Select|Wrap|Line Numbers
  1. line.getChars(19,line.length(),cardName,0);
  2.  
Best of Luck with your Try!

Kind regards,
Dmjpro.[/QUOTE

I get the characters I need from the string but the char[].toString() print jargon in the file. See below
Expand|Select|Wrap|Line Numbers
  1.  nameWriter.writeChars(pan.toString());
Sep 25 '07 #7

dmjpro
100+
P: 2,476
Print it separately.
pan.toString()
See what it prints.
What you tried to do, do it using substring.
Have a look at this.

Expand|Select|Wrap|Line Numbers
  1. String pan = null, cardName = null;
  2. //line.getChars(0,19,pan,0);
  3. pan = line.substring(0,19);
  4. //line.getChars(19,42,cardName,0);
  5. cardName = line.substring(19);
  6. //nameWriter.writeChars(pan.toString());
  7. nameWriter.writeChars(pan);
  8.  
Good Luck :-)

Kind regards,
Dmjpro.
Sep 25 '07 #8

P: 31
Print it separately.
pan.toString()
See what it prints.
What you tried to do, do it using substring.
Have a look at this.

Expand|Select|Wrap|Line Numbers
  1. String pan = null, cardName = null;
  2. //line.getChars(0,19,pan,0);
  3. pan = line.substring(0,19);
  4. //line.getChars(19,42,cardName,0);
  5. cardName = line.substring(19);
  6. //nameWriter.writeChars(pan.toString());
  7. nameWriter.writeChars(pan);
  8.  
Good Luck :-)

Kind regards,
Dmjpro.

Thanks it works but I have spaces in between the characters. How do I get rid of them?
Sep 25 '07 #9

dmjpro
100+
P: 2,476
Thanks it works but I have spaces in between the characters. How do I get rid of them?
Nice to see that you got your code worked.
I think you better to write a method which will do that.
Start writing Code then we are here to help you.
Please post that problem in separate if you need help.
Good Luck!

Kind regards,
Dmjpro.
Sep 25 '07 #10

P: 31
Hello. I am trying to write to a file from a file. The problem is the position of the characters I need to write are not always in the same for each line of data.
However, the number of preceeding commas before the characters is consistent.

Any suggestions on how to perform this task?
Sep 27 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.