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:

  1. 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 an int ?

  2. how do i int src.read() from src.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?

  • 2
    InputStream and OutputStream are abstract classes. The OutputStream has an abstract write (int b) method. You use your method with some non-abstract classes, hence you get the write (int b) implementation of the OutputStream . To get an array of bytes, you can use the int read(byte[] b) method of the InputStream . - post_zeew
  • >>>> To get an array of bytes, you can use the int read method (byte [] b). <<<< How to get an array of bytes if it accepts an array of bytes and returns an int? - Pavel
  • one
    @Pavel he returns the number of bytes actually read. The fact is that the array can be more than the number of bytes to read, then it will not fill up completely. - faoxis
  • one
    @Pavel, you pass a buffer to the read(...) method - byte[] b , that’s where the read data will be. - post_zeew
  • @Pavel was in a hurry yesterday, so he read your task inattentively. Your answer below added a solution. - faoxis

2 answers 2

InputStream - An abstract class for reading bytes. His read method is overloaded. If you use it without arguments, it will return a byte extended to int . If you pass an array of bytes into it, it will return the number of real bytes read.

OutputStream - An abstract class for writing bytes. His write method is overloaded and can accept a byte array or one byte , which automatically expands to an int. Returns nothing.

You can read more about them here and here .

Here is a good video lecture on this topic in Russian.

About your task ... What prevents you from reading the stream byte, then make a string of them ( new String(bytes[] )? Next, you can call the replaceAll method. For example, this method will return a string without a word : mystring.replaceAll("word", "") . Next, you call the getBytes method on the received string and you can do whatever you want with the received bytes, for example, write to the output stream.

I will demonstrate my idea with an example. I have a file fil1.txt with the following contents:

 This word has to be deleted! 

I run it through this test class:

 public class Test { public static void main(String[] args) throws IOException { FileInputStream fileInputStream = new FileInputStream("/home/someone/file1.txt"); FileOutputStream fileOutputStream = new FileOutputStream("/home/someone/file2.txt"); dropAbuses(fileInputStream, fileOutputStream); fileInputStream.close(); fileOutputStream.close(); } static void dropAbuses(InputStream src, OutputStream dst) throws IOException { byte bytes[] = new byte[src.available()]; src.read(bytes); String stringWithoutWord = new String(bytes).replaceAll("word ", ""); dst.write(stringWithoutWord.getBytes()); dst.flush(); } } 

Content file2.txt :

 This has to be deleted! 

The word gone. As required. :)

    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 an int ?

    OutputStream is an abstract class whose write(...) method has three signatures:

    1. void write(byte[] b) ;
    2. void write(byte[] b, int off, int len) ;
    3. abstract void write(int b) .

    In your method:

     void dropAbuses(InputStream src, OutputStream dst, String[] words) 

    as dst you pass some non-abstract subclass of the class OutputStream , which implements the void write(int b) method, which is why you can pass an int parameter to the write(...) method.

    For example, if you pass a DataOutputStream class object to your method as a dst , then when you call dst.write(5) the void write(int b) DataOutputStream class will be called.

    how do i int src.read() obtained from src.read() again into an array of bytes.

    Byte array? What for? The read() method of class InputStream reads one byte and returns its int -representation.

    To get an array of bytes from the InputStream , you can use one of the following methods:

    1. int read(byte[] b) ;
    2. int read(byte[] b, int off, int len) ;

    When using these methods, the read bytes will be written to byte[] b , and these methods will return the number of bytes read.

    • >>> To the byte array? What for? The InputStream class's read () method reads one byte and returns its int-ov representation. <<< And if the character does not fit in one byte, UTF8 encodes the Cyrillic character in two bytes. And with this approach, all characters larger than the 1st bit are cut off from me and then question marks instead ... - Pavel
    • one
      @Pavel, For reading characters it is more expedient to use InputStreamReader . - post_zeew
    • Yes, but how do I bring an InputStreamReader to an InputStream? After all, InputStreamReader is a successor to Reader. And I need to send the InputStream object to the method in the method ... This is a condition of my task. - Pavel
    • one
      @Pavel, Well, if мне надо , you can get away with the InputStream . I spoke only about expediency. - post_zeew
    • Yes, I completely agree with you, without any irony ... They just gave this assignment, nothing can be done about it. I think how can I collect all this now. There's also a condition that I have to do all this in a stream without saving anywhere ... In general, tin (((... - Pavel