-1

Suppose, I have a combio.txt file on my Machine which contains some string values as:

q
gjk
54Agh
56@hjgf
65g
@uyt&ht@
yt2

Note:- All values are Separated as New Line.

So want a code (Especially Java) which will return the strings whose length is less than 4 on consol.

Extension to This, All Strings values whose length is less than 4 should get deleted from the same file. OR Code should be able to separate the String values whose length is less than 4 in one newly created file and Greater than 4 to another newly created file.

  • 2
    Welcome to Stack Overflow. Please read [ask] and provide a [mre]. The purpose of Stack Overflow is not for us to do your work, but to help you with any problems you find while doing it yourself. So please show us your attempt at solving the task if you need help getting it to work. – JustAnotherDeveloper Sep 02 '20 at 09:23
  • https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#length() – Jayan Sep 02 '20 at 09:25
  • Does this answer your question? [How to count length of words in a File? Java](https://stackoverflow.com/questions/31759771/how-to-count-length-of-words-in-a-file-java) – Marc Sep 02 '20 at 09:27

1 Answers1

0

Using Files.lines from NIO will get every line.
Then filtering out the ones with more than 4 chars.
Then printing it to System.out.

String fileName = "combio.txt";
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
    stream.filter((String line) -> line.length() < 4).forEach(System.out::println);
} catch (IOException e) {
    e.printStackTrace();
}

This code prints lines with less than 4 chars on console.
You need to print them in a new File following this post

IQbrod
  • 2,060
  • 1
  • 6
  • 28