String check = "word"; void dropAbuses(InputStream src, OutputStream dst) throws IOException { while (true) { int element = src.read(); if (element != -1) { dst.write(element); } else { break; } } } I have this code here, but I can’t understand a few things that boil down to one thing:
If the
.write(byte[])method requires an array of bytes as an argument (which is quite logical and the development environment highlights it), then how does it agree that I send it anint?how do i
intsrc.read()fromsrc.read()again into an array of bytes?
Just why I ask? I need before I make the operation dst.write(element) to check that the "word" does not enter there and if it is included then do not copy and skip.
So now the method simply copies and I need a filter method and the question is how do I implement this filtering? The first thing that comes in is to break the "word" into bytes byte[] bytes = "word".getBytes() and compare it with the result of src.read() and it returns an infection int! And what to do ???
Of course, you can try to present the word as int . But still, really, I can't read the result from the InputStream , the interface intended for working with byte streams, to be represented as an array of bytes? Help me figure out what I misunderstand?
void dropAbuses(InputStream src, OutputStream dst, String[] words) { try (Close close = new Close()) { int[] forCheck = new int[words.length]; for (int i = 0; i != words.length; i++) { forCheck[i] = new ByteArrayInputStream(words[i].getBytes("UTF8")).read(); } while (true) { boolean equal = true; int element = src.read(); if (element != -1) { int i; for (i = 0; i != words.length; i++) { if (forCheck[i] == element) { equal = false; break; } } if (equal) dst.write(element); } else { break; } } } catch (IOException e) { e.printStackTrace(); } } Yes, I had to do it this way (((but still it doesn’t delete it with words in separate letters. But it doesn’t work with words. And by the way, only the English alphabet renderer can anyone know what the nuance is?
InputStreamandOutputStreamare abstract classes. TheOutputStreamhas an abstractwrite (int b)method. You use your method with some non-abstract classes, hence you get thewrite (int b)implementation of theOutputStream. To get an array of bytes, you can use theint read(byte[] b)method of theInputStream. - post_zeewread(...)method -byte[] b, that’s where the read data will be. - post_zeew