0

you started off with two pieces of code, one class that read a file, one that wrote a file.

start with the one that reads the file, and try putting that single part of the functionallity in one separate method, that stores every single line you read in an ArrayList and return that list later on.

here's something to get you started:

public static ArrayList<String> getFileContents(String fileName)
  throws IOException{
  ArrayList<String> contents = new ArrayList<String>();
  // here you add your code, based on the code in your first main method
  return contents;
}

whether this method is to be static or not, depends whether you will instantiate this class or not.
on a method like this, you can throw an Exception, since you still can catch it in the main method, but the main method is the very last place where you can catch and gracefully handle any exception.

0

you started off with two pieces of code, one class that read a file, one that wrote a file.

start with the one that reads the file, and try putting that single part of the functionallity in one separate method, that stores every single line you read in an ArrayList and return that list later on.

here's something to get you started:

public static ArrayList<String> getFileContents(String fileName)
  throws IOException{
  ArrayList<String> contents = new ArrayList<String>();
  // here you add your code, based on the code in your first main method
  return contents;
}

whether this method is to be static or not, depends whether you will instantiate this class or not.
on a method like this, you can throw an Exception, since you still can catch it in the main method, but the main method is the very last place where you can catch and gracefully handle any exception.

I'mma go youtube first. With it.

0

I'mma go youtube first. With it.

if you mean you'll stop here and try studying of a youtube video, good luck.
do remember, you can't ask that video for extra information, or to look after your code.

well, I hope it'll work out with you, but if this thread is to hard to follow, the youtube video might be even worse.

0

if you mean you'll stop here and try studying of a youtube video, good luck.
do remember, you can't ask that video for extra information, or to look after your code.

well, I hope it'll work out with you, but if this thread is to hard to follow, the youtube video might be even worse.

:| Still having a problem.

0

:| Still having a problem.

Even a Google on reading and writing to text files should help you hey.... start your code from the beginning because its just confusing you more. Think of it logically, first you would read the file, get the data you want do what's needed to the data then write to the file the new data. or maybe the files empty is which case first you have to write data then only can it be read your code example then might be:

public class Test {

    public static void main(String[] args) {
        String data = read("c:\t.txt");
        if (data != null) {
            //edit data as needed
            if (write("c:\t.txt", data)) {//write was success
            } else {//wri tefailed
            }
        }
    }

    private static String read(String fileName) {
        String stringdata = null;
        try {
            //read file here
        } catch (Exception e) {
            return stringdata = null;
        }

        return stringdata;//all data read
    }

    private static boolean write(String fileName, String dataToWrite) {
        try {
            //write to file here
        } catch (Exception e) {
            return false;
        }

        return true;//or false if file couldnt be written
    }
}//End of Class Test

you would then have the methods for reading and writing independent of each other and can be called at any time

Edited by DavidKroukamp: n/a

0

Even a Google on reading and writing to text files should help you hey.... start your code from the beginning because its just confusing you more. Think of it logically, first you would read the file, get the data you want do what's needed to the data then write to the file the new data. or maybe the files empty is which case first you have to write data then only can it be read your code example then might be:

public class Test {

    public static void main(String[] args) {
        String data = read("c:\t.txt");
        if (data != null) {
            //edit data as needed
            if (write("c:\t.txt", data)) {//write was success
            } else {//wri tefailed
            }
        }
    }

    private static String read(String fileName) {
        String stringdata = null;
        try {
            //read file here
        } catch (Exception e) {
            return stringdata = null;
        }

        return stringdata;//all data read
    }

    private static boolean write(String fileName, String dataToWrite) {
        try {
            //write to file here
        } catch (Exception e) {
            return false;
        }

        return true;//or false if file couldnt be written
    }
}//End of Class Test

you would then have the methods for reading and writing independent of each other and can be called at any time

THANKS ALOT YOU HELP ME ALOT. Lemme fix it :D Gonna beep this post if its done.

0

he just summarized what we've been suggesting all the time. are you not familiar with working with methods and instances of classes?

0

he just summarized what we've been suggesting all the time. are you not familiar with working with methods and instances of classes?

Yeap, :(

0

Small suggestion re cOrRuPtG3n3t!x's code posted above:
in both catch blocks add an e.printStackTrace(); so that if something goes wrong you will know what the problem was.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.