-2

Sample data in csv file

##Troubleshooting DHCP Configuration
#Module 3: Point-to-Point Protocol (PPP)
##Configuring HDLC Encapsulation 

Hardware is HD64570

So i want to get the lines as

#Troubleshooting DHCP Configuratin
Module 3: Point-to-Point Protocol(PPP)
#Configuring HDLC Encapsulation

Hardware is HD64570  

I have written sample code

public class ReadCSV {

public static BufferedReader br = null;

  public static void main(String[] args) {

      ReadCSV obj = new ReadCSV();
    obj.run();

  }

  public void run() {





                String sCurrentLine;

                try {
                    br = new BufferedReader(new FileReader("D:\\compare\\Genre_Subgenre.csv"));

                    try {
                        while ((sCurrentLine = br.readLine()) != null) {
                            if(sCurrentLine.charAt(0) == '#'){

                            System.out.println(sCurrentLine);
                            }
                        }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }





 }

}

I am getting below error

##Troubleshooting DHCP Configuration #Module 3: Point-to-Point Protocol (PPP) ##Configuring HDLC Encapsulation Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(Unknown Source) at example.ReadCSV.main(ReadCSV.java:19)

Please suggest me how to do this?

user3727850
  • 143
  • 2
  • 3
  • 11

7 Answers7

1

Steps:

  1. Read the CSV file line by line
  2. Use line.replaceFirst("#", "") to remove the first # from each line
  3. Write the modified lines to an output stream (file or String) which suites you
Adheep
  • 1,585
  • 1
  • 14
  • 27
  • I think if you have a line like `stuff something #comment` it will replace the # – Athanor Jul 31 '14 at 09:09
  • Thanks Athanor for noticing, But since the requirement is unclear whether what the user wants, I stick to the assumption that he wants the first `#` to be replaced :) – Adheep Jul 31 '14 at 09:11
0

If the variable s contains the content of the CSV file as String

s = s.replace("##", "#");

will replace all the occurrencies of '##" with '#'

Enry_h2o
  • 31
  • 1
  • 1
0

You need something like String line=buffer.readLine()

Check the first character of the line with line.charAt(0)=='#'

Get the new String with String newLine=line.substring(1)

Athanor
  • 855
  • 1
  • 16
  • 34
0

This is a rather trivial question. Rather than do the work for you, I'll outline the steps that you need to take without gifting you the answer.

  1. Read in a file line by line
  2. Take the first line and check if the first character of this line is a # - If it is, create a substring of this line excluding the first character ( or use fileLine.replaceFirst("#", ""); )
  3. Store this line somewhere in an array like data structure or simply replace the current variable with the edited one ( fileLine = fileLine.replaceFirst("#", ""); )
  4. Repeat until no more lines left from file.
  5. If you want to add these changes to the file, simply overwrite the old file with the new lines (e.g. Using a steam reader and setting second parameter to false would overwrite)

Make an attempt and show us what you have tried, people will be more likely to help if they believe you have attempted the problem yourself thoroughly first.

gavlaaaaaaaa
  • 612
  • 2
  • 7
  • 11
0
package stackoverflow.q_25054783;

import java.util.Arrays;

public class RemoveHash {
    public static void main(String[] args) {
        String [] strArray = new String [3];
        strArray[0] = "##Troubleshooting DHCP Configuration";
        strArray[1] = "#Module 3: Point-to-Point Protocol (PPP)";
        strArray[2] = "##Configuring HDLC Encapsulation"; 
        System.out.println("Original array: " + Arrays.toString(strArray));

        for (int i = 0; i < strArray.length; i++) {
            strArray[i] = strArray[i].replaceFirst("#", ""); 
        }
        System.out.println("Updated array: " + Arrays.toString(strArray));
    }
}

//Output:
//Original array: [##Troubleshooting DHCP Configuration, #Module 3: Point-to-Point Protocol (PPP), ##Configuring HDLC Encapsulation]
//Updated array: [#Troubleshooting DHCP Configuration, Module 3: Point-to-Point Protocol (PPP), #Configuring HDLC Encapsulation]
Nikhil Joshi
  • 817
  • 2
  • 12
  • 34
0

OpenCSV reads CSV file line by line and gives you an array of strings, where each string is one comma separated value, right? Thus, you are operating on a string.

You want to remove '#' symbol from the beginning of the string (if it is there). Correct?

Then this should do it:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
    if (nextLine[0].charAt(0) == '#') {
        nextLine[0] = nextLine[0].substring(1, nextLine[0].length());
    }
}

Replacing the first '#' symbol on each of the lines in the CSV file.

0
private List<String> getFileContentWithoutFirstChar(File f){
    try (BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(f), Charset.forName("UTF-8")))){
        List<String> lines = new ArrayList<String>();

        for(String line = input.readLine(); line != null; line = input.readLine()) {
            lines.add(line.substring(1));
        }

        return lines

    } catch(IOException e) {
        e.printStackTrace();
        System.exit(1);
        return null;
    }
}

private void writeFile(List<String> lines, File f){
    try(BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), StandardCharsets.UTF_8))){
        for(String line : lines){
            bw.write(content);
        }
        bw.flush();
    }catch (Exception e) {
        e.printStackTrace();
    }
}

main(){
    File f = new File("file/path");
    List<Stirng> lines = getFileContent(f);
    f.delete();
    writeFile(lines, f);
}
gkiko
  • 2,283
  • 3
  • 30
  • 50