1

I have a method to copy the entire file from one destination to another destination using buffer:

InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
   out.write(buf, 0, len);
}

in.close();
out.close();

The file is in csv format:

"2280B_TJ1400_001","TJ1400_Type-7SR","192.168.50.76","Aries SDH","6.0","192.168.0.254",24,"2280B Cyberjaya","Mahadzir Ibrahim"

But as you can see it has quotes inside it. Is it possible remove them by based on my exisitng code???

Output should be like this:

2280B_TJ1400_001,TJ1400_Type-7SR,192.168.50.76,Aries SDH,6.0,192.168.0.254,24,2280B Cyberjaya,Mahadzir Ibrahim
Arnold Cristobal
  • 843
  • 2
  • 16
  • 36
  • possible duplicate of [Unwanted double quotes in generated csv file](http://stackoverflow.com/questions/13969254/unwanted-double-quotes-in-generated-csv-file) – yogesh Sep 30 '14 at 08:53
  • It uses csvreader which i believe needs opencsv. Correct me if I'm wrong but that library does not work in jdk 1.6 (deploy my exe jar) that's why im using the buffer. :) – Arnold Cristobal Sep 30 '14 at 08:58

4 Answers4

3

If you use a BufferedReader you can use the readLine() function to read the contents of the file as a String. Then you can use the normal functions on String to manipulate it before writing it to the output. By using an OutputStreamWriter you can write the Strings directly.

An advantage of the above is that you never have to bother with the raw bytes, this makes your code easier to read and less prone to mistakes in special cases.

BufferedReader in = new BufferedReader(new  InputStreamReader(new FileInputStream(src)));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(dest));
String line;
while ((line = in.readLine()) != null) {
   String stringOut = line.replaceAll("\"", "");
   out.write(stringOut);
}

in.close();
out.close();

Note that this removes all " characters, not just the ones at the start and end of each String. To do that, you can use a StringTokenizer, or a more complex replace.

Thirler
  • 20,239
  • 14
  • 63
  • 92
0

Not sure it's a good idea or not, but you can do something like :

 while ((len = in.read(buf)) > 0) {
     String temp = new String(buf);
     temp = temp.replaceAll("\"","");
     buf = temp.getBytes();
     len = temp.length();
  out.write(buf, 0, len);
 }
blackSmith
  • 3,054
  • 1
  • 20
  • 37
0

For me, I would read all the file before, in a String, and then strip out the ' " ' in the string. Then write it to the dest file.

Read the file in a string

I found this simple solution. This may not be the best depending on your level of error catching you need.But it's working enough ;)

String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();

Stripout the ' " '

content = content.replaceAll('"', "");

Write it to dest file from here

Files.write(Paths.get("./duke.txt"), msg.getBytes());

This is for java 7+. Did not test it but it should work !

Community
  • 1
  • 1
Nikkolasg
  • 444
  • 4
  • 18
0

Not necessarily good style, filtering quotes in binary data, but very solid.

Wrap the original InputStream with your own InputStream, filtering out the double quote.

I have added a quirk: in MS Excel a quoted field may contain a quote, which then is self-escaped, represented as two double quotes.

InputStream in = new UnquotingInputStream(new FileInputStream(src));

/**
 * Removes ASCII double quote from an InputStream.
 * Two consequtive quotes stand for one quote: self-escaping like used
 * by MS Excel.
 */
public class UnquotingInputStream extends InputStream {

    private final InputStream in;
    private boolean justHadAQuote;

    public UnquotingInputStream(InputStream in) {
        this.in = in;
    }

    @Override
    public int read() throws IOException {
        int c = in.read();
        if (c == '\"') {
            if (!justHadAQuote) {
                justHadAQuote = true;
                return read(); // Skip quote
            }
        }
        justHadAQuote = false;
        return c;
    }

}

Works for all encodings that use ASCII as subset. So not: UTF-16 or EBCDIC.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138