Remove carriage returns using Scanner

From MyWiki
Revision as of 22:31, 29 September 2017 by George2 (Talk | contribs)

Jump to: navigation, search

The headers of the Janet csv file

0 "Request ID",
1 "Common Name",
2 "Renewal Date",
3 "Requested By",
4 "Certificate Type",
5 "CSR SANs",
6 "Extra SANs",
7 "Certificate Expiry Address",
8 Status
File file = new File("yourFilePath.txt");  // create File object to read from
Scanner scanner = new Scanner(file);       // create scanner to read
Printwriter writer = new PrintWriter("someOutputFile.txt"); // create file to write to
 
while(scanner.hasNextLine()){  // while there is a next line
    String line = scanner.nextLine();  // line = that next line
if ( line.toLowerCase().contains("certificate type") { continue;}
    // do something with that line
    String newLine = "";
 
    // replace a character
    for (int i = 0; i < line.length(); i++){
        if (line.charAt(i) != '*') {  // or anything other character you chose
            newLine += line.charAt(i);
        }
    }
 
    // print to another file.
    writer.println(newLine);
}