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

how to split a text file

 
naga raaju
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai guys,

i am new to java language,i want to read a text file and split that file and store it in database.

raju.txt

Our records indicate that you have never posted to our site before! Why


this is the text file i want to read using
FileInputStream fstream = new FileInputStream("test.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));

like this i wnat to split this raju.txt is like

1)ourrecords -10(characters)
2)indicate -8 characters
3)taht-4 characters


like taht ,please freinds any body give idea to split the file according to my requirement.

thanks in advance

bye
Naga
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Probably easier to use a Scanner, which automatically splits using a regular expression.
Put the name of the file into the constructor of the Scanner, and leave out the 2nd argument. Then the Scanner will split on its default delimiter (=whitespace).
Use the hasNext() method as the test for a while loop. Use the next() method to read the next token inside the while loop
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

For starters, you should not use a DataInputStream for text files. Just construct the InputStreamReader with fstream.

From a BufferedReader you will get a String object for each line of text. You can then use the String.split method to cut that line into pieces according to your requirements.
 
naga raaju
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for misstyped

raju.txt

Our records indicate that you have never posted to our site before! Why


this is my test file input what my out put is

ourre
cordsin
dicatet

like that not spaces split.

is there any possible to split like this using scanner class.

thanks in advance
bye
Naga
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you post the code you're using we may be able to point out where it's going wrong.
 
naga raaju
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
import java.util.Scanner;
public class FileRead {

public static void main(String[] args) throws IOException
{

File file = new File("test.txt");
try {

Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();

Scanner lineScanner = new Scanner(line);
lineScanner.useDelimiter("");
while (lineScanner.hasNext()) {

String part = lineScanner.next();
System.out.print(part + " ");
}
System.out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

this is my code .part is print the gap beteween characters .
what my question is how to divide first five characters in one string,next 8 characters in another sting,next 7 characters in another sting.
i don't have any idea abt that so please give the code for that.actually that is wrong asking but no idea in my mind so plase help me.

bye
naga
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner uses delimiter characters. If that's not how you want to divide up the string, use the BufferedReader method you had before (and which I commented on). That will give you each line of the file as a string. Then you can use the String methods (like substring) to carve out the pieces you're interested in.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your problem is that you are using the wrong delimiter. I said to use the default delimiter, so you oughtn't to call the useDelimiter() method. You are also reading the first word from each line, so you will have to read several times in the while loop. Try again with those changes and I think you will get what you want.

Alternative: When your Scanner (or your BufferedReader) reads a line, it is a String. Use the split() method of String (again without specifying a delimiter) and you get the line divided into an array of Strings.
 
Susanta Mandal
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi naga raaju
Try the following code(after modifying your code), i think it will work as your requirement,if not then modify some more like this using String or StringBuffer class methods.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Susanta Mandal, but please don't simply give out answers like that. Read this.

Your answer is actually not very efficient; you don't need two Scanners.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic