#Get Input Stream of Characters?!
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
Yes, it is possible to have an input stream of characters in Java. You can use the Reader class to read characters from an input source.
Here's an example of how you can create an input stream of characters and iterate over them one character at a time:
import java.io.*;
public class CharacterInputStreamExample {
public static void main(String[] args) {
try {
// Create a FileReader to read characters from a file
Reader reader = new FileReader("input.txt");
// Create a BufferedReader for efficient reading
BufferedReader bufferedReader = new BufferedReader(reader);
int character;
while ((character = bufferedReader.read()) != -1) {
// Process each character here
char c = (char) character;
System.out.print(c);
}
// Close the reader
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In this example, we use a FileReader to read characters from a file. The BufferedReader wraps the FileReader for efficient reading. The read() method of the BufferedReader returns the next character as an integer value, which we then cast to a char.
You can replace "input.txt" with any other source of characters like a network socket or standard input (System.in) depending on your requirements.
I'm starting with a Scanner of a txt file
I want to essentially iterate over it character by character, like a film tape over characters
that being said, the chatgpt answer might be good
I'mma try that
Its better if you use NIO
As specially the Files class
Then you can get all lines in just one go without FileReader, BufferedReader etc
but I need an api that goes through character by character like a film tape
where I don't have to iterate over lines and then iterate over characters
it makes my logic more complex than it needs to be
You can stream all characters
Of course based on what u plan to do with the chars
With flatMap you can combine all lines into one and then do something with it
Arrays.stream(lines).flatmap(...).foreach(...);
I had a post about for each before
it's really shitty to work with when you want to mutate state from parent scope
it just doesn't allow you to do it
which makes it unusable in my situation
thats wrong
the solution is better
and you are not forced to use forEach
use toList on the Stream and then iterate over it using a normal for loop
that way you wont have the problem with lambda
I know, but that defeats the efficiency of a stream
there's a clear performance loss when converting to a list
for files that are potentially hundreds of thousands of lines long, it would blow up
that you shouldnt care at all
nah
it's not premature optimization it's trying to be aware of java's strenghts and weaknesses
in rust the default way I'd do things IS the efficient way
it's not premature optimization if its more convenient as well
but we are not in rust
it is premature optimization
You have no idea if it would actually cause problem or if it won't be noticable at the first place
and you haven't explained your use case, why do you want to do that at the first place ?
@tropic domehttps://discord.com/channels/272761734820003841/1198302493583614012