简体   繁体   中英

Read from text file with a condition

I'm reading from a text file with a condition that words starting with * are to be ignored.

example:
abc 1234 *text to be ignored

So in this example, I will ignore "text to be ignored" when reading from text file and will only store abc and 1234 in string array.

For this, I have written below code. How can I achieve the condition to ignore words starting with * ?

public static void read(String filename) {
        BufferedReader reader = null;

        try {
            String line;
            reader = new BufferedReader (new FileReader(filename));
            while ((line = reader.readLine()) != null) {
                String[] functionName = line.split("\\s+");         
                            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (reader != null)
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }

startWith(String literal) returns true if your String start with the given string literal.

For Example :

"1234".startsWith("12"); returns true .

So you should read all the words and check if it starts or even contains *, if so, then ignore the whole word.

Example :

if(! word.startsWith("*")) {
// add to what ever you want
}

or

if(! word.contains("*")) {
// add to what ever you want
}

You can try indexOf() with substring() like

 while ((line = reader.readLine()) != null) {
    if(line.indexOf("*")>-1)
    line=line.substring(0,line.indexOf("*"));
    String[] functionName = line.split("\\s+");  
 }

what the above indexOf("*") will give you the index of * then you can just find the substring with with endIndex as the index of * you found by indexOf("*") by doing substring(beginIndex,endIndex)

You can do something like in your while loop -

while ((line = reader.readLine()) != null) {
   String[] functionName = line.split("\\s+");         
   String newLine = "";

   for(String strg : functionName){

      if(strg.startsWith("*")){
         break;
      }else{
         newLine = strg + newLine;
      }

   }
}

You don't tell what version of Java you are using so I'm going to assume Java 8...

NOTE: code is untested but it should work with some adaptations.

private static final Pattern SPACES = Pattern.compile("\\s+");
private static final Pattern STAR_TO_END = Pattern.compile("\\s*\\*.*");
public static String[] read(final String filename)
{
    final Path path = Paths.get(filename);

    try (
        // UTF-8 by default; excellent
        final Stream<String> lines = Files.line(path);
    ) {
        return lines.map(line -> STAR_TO_END.matcher(line).replaceFirst(""))
            .flatMap(SPACES::splitAsStream)
            .collect(Collectors.toArray(String[]::new));
    }
}

If you dont want to loop through your words to check if it starts with a * you could also remove all the words with asterisks in from of them prior to using split .

String str = "abc 1234 *text to be ignored";
System.out.println(Arrays.toString(str.replaceAll("\\*[^\\s]+\\s*", "").split("\\s+")));
// [abc, 1234, to, be, ignored]
str = "*abc *1234 *text to be *ignored";
System.out.println(Arrays.toString(str.replaceAll("\\*[^\\s]+\\s*", "").split("\\s+")));
// [to, be]

Regex breakdown

\\* - Literal match of asterisk
[^\\s]+ - Match anything but a space
\\s* - Capture any or no spaces at end of word

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM