File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using .length()

 
jake mullet
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello JavaGurus, I need to know if everyfile I read in has a 'Z' contained within the last line of the file. in particular it is the 10th byte from the EOF, but each file is different in length, so I'm using the .length() to get the total size but how would I say sBuffer.length() - 10 , then get the charAt() to check if it is a 'Z' thanks for anyone's help
import java.io.*;
import java.util.*;

public class injRecFix{
static PrintWriter prntWrter;

public static void main(String[] args){
char[] chBuffer =null;
boolean bReadFile = false;
String sINFileName = null;
String sOUTFileName = null;
int i;

InputStream fin;
File outputFile = new File("newPunchFile.txt");

if(sINFileName == null)
sINFileName = "Punch.001";

try {
fin = new FileInputStream(sINFileName);
prntWrter = new PrintWriter(new FileWriter(outputFile));
} catch (IOException except) {
sINFileName = "Punch.001";
try {
fin = new FileInputStream(sINFileName);
} catch (IOException eIO) {
fin = null;
}
}
if(fin != null) { // dumping PunchFile into chBuffer
try {
chBuffer = new char[fin.available()];
int iC = 0;
while(iC < chBuffer.length) {
i = fin.read();
chBuffer[iC] = (char)i;
iC++;
}
fin.close();
bReadFile = true;
} catch (IOException except) {
bReadFile = false;
}
}
if (bReadFile) {
// Convert buffer into a string.
String sBuffer = new String(chBuffer, 0, chBuffer.length);
sBuffer.charAt(??)
//System.out.println(sBuffer.length());
}
}//end main()
}//end class injRecFix
 
Richard Boren
Ranch Hand
Posts: 233
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not a guru, but String has a method indexOf() that will return the location of the string or character passed to it. If non is found it returns a -1.
 
Art Metzer
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Jake. Let me preface all this by saying, I'm not a Java guru, either.

I should note that the contents of C:\zonk.txt are

The method match() goes to the tenth byte from the end of the file and compares that byte to the byte representation of Z, and returns true if they match, false if they don't.
It's not the soundest solution, but I think it'll work. Hope it helps.
Art
[This message has been edited by Art Metzer (edited May 09, 2001).]
 
Susan Delph
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm definitely NOT a Java Guru, but....
lastIndexOf('z') will find the last occurrence of 'z' in the String.
int index = sBuffer.lastIndexOf('z');
This will search the String backwards, and index will be set to whatever position the final 'z' of the String is found in.
(If no 'z' is found, index is set to -1, as Richard explained above.)
Susan

[This message has been edited by Susan Delph (edited May 09, 2001).]
 
Susan Delph
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, dump my above suggestion. This is better -- in my opinion:

It printed out 'true' when I ran this.
Susan

[This message has been edited by Susan Delph (edited May 09, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic