aspose file tools*
The moose likes Beginning Java and the fly likes how to split a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to split a text file " Watch "how to split a text file " New topic
Author

how to split a text file

naga raaju
Greenhorn

Joined: Mar 14, 2008
Posts: 29
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

Joined: Oct 13, 2005
Posts: 36599
    
  16
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
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
naga raaju
Greenhorn

Joined: Mar 14, 2008
Posts: 29
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
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27
If you post the code you're using we may be able to point out where it's going wrong.
naga raaju
Greenhorn

Joined: Mar 14, 2008
Posts: 29
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
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27
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

Joined: Oct 13, 2005
Posts: 36599
    
  16
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

Joined: Feb 25, 2008
Posts: 1
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

Joined: Oct 13, 2005
Posts: 36599
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to split a text file
 
Similar Threads
Why do i get NoClassDefFound error??
to read lines inside brackets
read and write from txt file
command line arguments for a file
Check for duplicates in database before insert