File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to divide File in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to divide File in Java" Watch "How to divide File in Java" New topic
Author

How to divide File in Java

shaza smart
Greenhorn

Joined: May 21, 2011
Posts: 5
Hello Guz. .

Can you help me in my project ..
Draft as follows


>>>
The File chopper takes one file as an input and divides it into a number of files
each file containing x lines. The interface takes the file name as input and the
number of lines, each of the output files should contain. For example, if you give a
file with 20 lines as input and enter 4 as number of lines. Then 5 files will be
generated, each containing only 4 lines of the input file.


My question is how to divide the file to a few lines and I want to create a new file to keep the lines divided by the number that the user enter it
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41108
    
  45
You'll need to use several classes from the java.io package for this. FileReader, BufferedReader, FileWriter and BufferedWriter might all be involved.

Note that FileReader and FileWriter use the platform default encoding, so make sure that's what the text files use. If they use some other encoding, then you'd have to use FileInputStream and InputStreamReader instead of FileReader, and the corresponding classes for writing the file. Those allow you to specify the encoding explicitly.


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
. . . and welcome to the Ranch
shaza smart
Greenhorn

Joined: May 21, 2011
Posts: 5
Campbell Ritchie wrote: . . . and welcome to the Ranch


Thank you ^^
shaza smart
Greenhorn

Joined: May 21, 2011
Posts: 5
Ulf Dittmer wrote:You'll need to use several classes from the java.io package for this. FileReader, BufferedReader, FileWriter and BufferedWriter might all be involved.

Note that FileReader and FileWriter use the platform default encoding, so make sure that's what the text files use. If they use some other encoding, then you'd have to use FileInputStream and InputStreamReader instead of FileReader, and the corresponding classes for writing the file. Those allow you to specify the encoding explicitly.



Hello .. Thank you to help you but I know this
This is my program and started it, but I have remaining small problem ..
How do I link the counter (longer lines of the file) to the number entered by the user so divided by it and creates a new file on the numbering.

<<


>>>>>
Please help me the deadline to May 28
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

It would probably be a good idea if you finished the action listener for the rest of your buttons, i.e. 'number of lines:' and 'Run'.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
shaza smart
Greenhorn

Joined: May 21, 2011
Posts: 5
Sorry ..Do you know How do I create new files are divided depending on the number and stored within the lines divided?
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

The number of files that you need to create is the total # of lines from the source file divided by the number of lines the user wants per file. So you would just need to loop through the source file that number of lines and write those to a different file.

Hunter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
You should be able to count the lines as you read, and create new files as you go.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

The goal of the forum is to help poster's create their own solution, not give them solutions. No one learns anything that way.

Hunter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Agree. But I can remove the post so it does no harm.
Udara Amarasinghe
Ranch Hand

Joined: Aug 17, 2009
Posts: 109

Sorry if I make wrong here . But I just try to help Shaza.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
apologies accepted
shaza smart
Greenhorn

Joined: May 21, 2011
Posts: 5
Thank you all
I do not want a solution because it's my project and I want to resolve it myself, but because the Boost time my thoughts really confused and I can not focus

Thank you
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

My advice to you is:

1. Write code that will read the data from one file and write it to another, one line at a time. Once you have that is is simpler to modify to get what you want.
2. Next modify the code to keep a count of the rows as you add them to your file.
3. Modify so that when the count reaches a certain number it closes your current output file, creates a new file with a different filename resets the count and starts writing to the new file. (You may want filenames such as file1.txt, file2.txt... so on, so you may want to also keep a count of which file you were on)

Try this, if you are having trouble while following this then post the appropriate code and we will help you based on that.

Hope that helps.

Sean


I love this place!
Udara Amarasinghe
Ranch Hand

Joined: Aug 17, 2009
Posts: 109

Hi shaza,

I agree with Seans advises. Are you still confused? and what kind of help you need?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to divide File in Java
 
Similar Threads
shell script to get the end of line
Binary to Decimal conversion
File splitting with Java..Help
Reading your Data File
Help! Problems with a method calling itself...