Remove carriage returns using Scanner

From MyWiki
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) != "\r") {  // or anything other character you chose
            newLine += line.charAt(i);
        }
    }
 
    // print to another file.
    writer.println(newLine);
}

Get rid of carriage returns ( not really any good )

char c;
int cint;
for(int n = 0; n < str.length(); n ++;)
{
    c = str.charAt(n);
    cint = (int)c;
    if(cint <48 || (cint > 57 && cint < 65) || (cint > 90 && cint < 97) || cint > 122)
    {
        specialCharacterCount++
    }
}

ByteStream tutorial - https://docs.oracle.com/javase/tutorial/essential/io/bytestreams.html

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class CopyBytes {
    public static void main(String[] args) throws IOException {
 
        FileInputStream in = null;
        FileOutputStream out = null;
 
        try {
            in = new FileInputStream("xanadu.txt");
            out = new FileOutputStream("outagain.txt");
            int c;
 
            while ((c = in.read()) != -1) {
                out.write(c);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}