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;
}
}