0

I want to send each line one at a time to the calling method from a text file.

I am trying to read a file line by line and populated each line to a string array. i.e. each line is considered to be one element in string array. Can anyone please help me how to achieve. But after doing this I an unable to send individual elements from the array to the calling function.. Can anyone please help me how to achieve this

package fileReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class stringBuffer{

    public static void main(int args[])
    {
        String line = readFile();
        writeFile(line);

    }

    public static String readFile()
    {
        String inputfile = null;
        Scanner sc = new Scanner(System.in);
        String[] temp = new String[100];
        System.out.println("Please enter the input file location C:\\text.txt");
        inputfile = sc.nextLine();
        Scanner r = null;
        try
        {
            r = new Scanner(new File(inputfile));
            String i = null;
            for (int x;r.hasNextLine() && (i = r.nextLine()) != null; x++ ) {
                System.out.println(i);
                temp[x] = i;

            }
            r.close();
            if(temp == null) return null;
            String str[] = new String[temp.length];
            for(int y =0; y < temp.length; y++)
            {
                // How to send each element to the called function here str[i] = 
            }

        }
        catch (FileNotFoundException ex) 
            System.out.println("The file could not be found! "+ex.getMessage());                  
        return null;
    }
}
Wojciech Wirzbicki
  • 3,887
  • 6
  • 36
  • 59
cartick
  • 1
  • 2

1 Answers1

0

If you want to send each line back to the function that called it, you'll have to send back the entire array or as @MJSG said, call writeFile() in the readFile() method.

So either:

  • change it to public static String[] readFile() and loop through the elements in main, calling writeFile(lines[i]) for each OR
  • Change the end of readFile() such that it calls writeFile(temp[y])
  • Hi, I took your suggestion and changed the readFIle() method as static and in the main method Implemented a For loop for every line and called writeFile() method. But the writeFile method overwrites with the latest line but does not write all the sentences. Can you please suggest how to achieve this – cartick Apr 06 '15 at 22:59
  • public static void writeToFile(String output) { String fileName = "output.txt"; try { FileWriter fw = new FileWriter(fileName); BufferedWriter bw =new BufferedWriter(fw); bw.write(output); bw.newLine(); bw.close(); } catch(IOException ex) { System.out.println("Error writing to file '"+ fileName + "'"); } } – cartick Apr 06 '15 at 22:59
  • I think your issue is that the write() method is not appending to the file, and instead overwrites. This might help: http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java –  Apr 06 '15 at 23:49