I need a java method public static String[] loadFile(String fname, String...line
ID: 3599782 • Letter: I
Question
I need a java method public static String[] loadFile(String fname, String...lineRange). lineRange show how many lines should be loaded.
We have to invoke method like this:
String array[] = loadFile("myfile.txt", "1-9");
String array[] = loadFile("myfile.txt", "^-$");// from first to last
String array[] = loadFile("myfile.txt", "7,7,7,7");// i want to load 7th line four times
String array[] = loadFile("myfile.txt", "-"); // it means i want to load all file
String array[] = loadFile("myfile.txt", "-5, -5, -5"); // i want to load file from start to 5 three times
String array[] = loadFile("myfile.txt", "5-1"); // i want to load file from 5 line to first.
String array[] = loadFile("myfile.txt", "$, $, $"); // 3 times last line....
and main method where we can test and print lines from file with different lineRange.
Focus how we can proceed lineRange exactly like String with "-" : "1-9", "5-1", "-2,-2,-2", "-", "^-$", "$,$,$", "7, 7, 7, 7, 7", etc
Thanks
Explanation / Answer
import java.util.*;
import java.io.*;
class loadFile {
public static String[] loadFile(String fname, String lineRange) {
/*Split the line range using comma*/
List<String> arguments = new ArrayList<String>(Arrays.asList(lineRange.split(",")));
/*Create a array list of strings to store the specified lines*/
List<String> lines = new ArrayList<String>();
/*line variable of type String to read line from file*/
String line = null;
/*if lineRange has only one argument*/
if(arguments.size() == 1) {
/*if only argument passed as lineRange is whole file or start to end*/
if (arguments.get(0).equals("-") || arguments.get(0).equals("^-$")) {//all file
try
{
/* FileReader reads text files in the default encoding */
FileReader fileReader = new FileReader(fname);
/* wrap the FileReader in BufferedReader */
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) /*while line read is not null*/
{
lines.add(line);/*add the line to the string list*/
}
/*close the file after use */
bufferedReader.close();
}
catch(IOException ex)
{
System.out.println("Error reading file named '" + fname + "'");
}
}
/*else if pattern matches with something like 12-15 or 18-11*/
else if (arguments.get(0).matches("^\d+-\d+")) {
/*split the pattern to get the range of lines*/
List<String> firstSecondLine = new ArrayList<String>(Arrays.asList(arguments.get(0).split("-")));
/*parse the first integer from the given string*/
int firstLine = Integer.parseInt(firstSecondLine.get(0));
/*parse the second integer from the given argument*/
int secondLine = Integer.parseInt(firstSecondLine.get(1));
/*calculate minimum of these as 1-9 and 8-1 both are valid*/
int min = Math.min(firstLine, secondLine);
/*if min is not equal to firstLine then lines should be loaded in reverse order*/
boolean reverse = false;
Stack<String> stack = new Stack<String>(); /*Stack to store strings in reverse order*/
if(min!=firstLine) {
reverse = true;
}
try
{
int lineCount = firstLine;
FileReader fileReader = new FileReader(fname);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null)
{
if(reverse) {
if(secondLine <= lineCount && firstLine >= lineCount) stack.push(line);
lineCount--;
}
else {
if(firstLine <= lineCount && secondLine >= lineCount) lines.add(line);
lineCount++;
}
}
if (reverse) {
while(!stack.empty()) lines.add(stack.pop());
}
bufferedReader.close();
}
catch(IOException ex)
{
System.out.println("Error reading file named '" + fname + "'");
}
}
}
else {
String lastLine = null;
for (int i = 0; i < arguments.size(); i++) {
if(arguments.get(i).equals("$")) {
if(lastLine == null) {
try
{
FileReader fileReader = new FileReader(fname);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null)
{
lastLine = line;
}
bufferedReader.close();
}
catch(IOException ex)
{
System.out.println("Error reading file named '" + fname + "'");
}
lines.add(lastLine);
} else lines.add(lastLine);
} else if(arguments.get(i).matches("^\d+")) {
int lineNumber = Integer.parseInt(arguments.get(i));
int lineCount = 1;
try
{
FileReader fileReader = new FileReader(fname);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null)
{
if(lineCount == lineNumber) lines.add(line);
lineCount++;
}
bufferedReader.close();
}
catch(IOException ex)
{
System.out.println("Error reading file named '" + fname + "'");
}
} else if(arguments.get(i).matches("-\d+")) {
int lineNumber = Integer.parseInt(arguments.get(i));
lineNumber = -1 * lineNumber; // to make it positive
int lineCount = 1;
try {
FileReader fileReader = new FileReader(fname);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
if(lineCount <= lineNumber) lines.add(line);
lineCount++;
}
bufferedReader.close();
}
catch(IOException ex) {
System.out.println("Error reading file named '" + fname + "'");
}
}
}
}
return lines.toArray(new String[0]);
}
public static void main(String[] args) {
String[] a = loadFile("text", "3-1");
System.out.println(Arrays.toString(a));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.