• 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

how to split a text file

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic