Skip to content

Instantly share code, notes, and snippets.

@ryasmi
Last active December 14, 2015 22:58
Show Gist options
  • Save ryasmi/5161866 to your computer and use it in GitHub Desktop.
Save ryasmi/5161866 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
public class FilterTextFile {
public static void main(String[] args) throws FileNotFoundException {
Scanner fScan, cScan = new Scanner(System.in);
String inFile, outFile, findStr;
FileInputStream fileInput;
PrintWriter fileOutput;
// Get input file.
System.out.print("Input file ? ");
inFile = cScan.nextLine();
fileInput = new FileInputStream(inFile);
fScan = new Scanner(fileInput);
// Get output file.
System.out.print("Output file ? ");
outFile = cScan.nextLine();
fileOutput = new PrintWriter(outFile);
// Get search string.
System.out.print("String to search for ? ");
findStr = cScan.nextLine();
// Filter input file to output file.
while (fScan.hasNextLine()) {
String line = fScan.nextLine();
if (line.contains(findStr)) {
fileOutput.println(line);
}
}
fileOutput.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment