• 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 divide File in Java

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
shaza smart
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: . . . and welcome to the Ranch



Thank you ^^
 
shaza smart
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
shaza smart
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be able to count the lines as you read, and create new files as you go.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree. But I can remove the post so it does no harm.
 
Ranch Hand
Posts: 109
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if I make wrong here . But I just try to help Shaza.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
apologies accepted
 
shaza smart
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Udara Amarasinghe
Ranch Hand
Posts: 109
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi shaza,

I agree with Seans advises. Are you still confused? and what kind of help you need?
reply
    Bookmark Topic Watch Topic
  • New Topic