i am trying to put this together so it opens a file sorts the data saves a file and appends a file i have got my self so confused. can any one help????
this is the code Ive got so far.

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;


public class studentData {


    public static void main(String[] args) {

        FileDemo fd = new FileDemo();


        String txt = fd.readFile();



 // write data to file report

    public void writeFile() {

        String fileName = "c:\\report.txt";


        try {

            BufferedWriter writer = new BufferedWriter(new FileWriter(fileName,true));

            writer.write("Test report.");




        } catch (IOException e) {




 // read data1 text file

    public String readFile() {

        String fileName = "c:\\data1.txt";

        String LS = System.getProperty("line.separator");

        StringBuffer fileContent = new StringBuffer();


        try {

            FileReader fr = new FileReader(fileName);

            BufferedReader reader = new BufferedReader(new FileReader(fileName));


            String line;

            while ((line = reader.readLine()) != null) {




        } catch (IOException e) {




        return fileContent.toString();


//Appending to a File 
try {
    BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));
} catch (IOException e) {
// Returns the contents of the file in a byte array.
public static byte[] getBytesFromFile(File file) throws IOException {
    InputStream is = new FileInputStream(file);

    // Get the size of the file
    long length = file.length();

    // You cannot create an array using a long type.
    // It needs to be an int type.
    // Before converting to an int type, check
    // to ensure that file is not larger than Integer.MAX_VALUE.
    if (length > Integer.MAX_VALUE) {
        // File is too large

    // Create the byte array to hold the data
    byte[] bytes = new byte[(int)length];

    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length
           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
        offset += numRead;

    // Ensure all the bytes have been read in
    if (offset < bytes.length) {
        throw new IOException("Could not completely read file "+file.getName());

    // Close the input stream and return bytes
    return bytes;

Here is the code of java program to Read text File Line by Line

import java.io.*;
class FileRead 
public static void main(String args[])
// Open the file that is the first 
// command line parameter
FileInputStream fstream = new FileInputStream("textfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
//Close the input stream
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());

Recommended Answers

All 2 Replies

Do the project one step at a time. You list 4 things you want to do:
opens a file
sorts the data
saves a file and
appends a file

Start with the first one. Can you do that ok? If not explain your problem
Then go to the second one. Can you do that ok? If not explain your problem
and then the same for the third one
and again for the fourth one.

Line 71 is not needed and you could do the same for line 11 in the second FileRead class.

Also on line 102, filename is within speechmarks.

The coding looks okay, do you have an error message?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.