English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

التعليمية الأساسية لـ Java

تحكم في العملية في Java

مجموعات Java Array

Java موجه الأعمال (I)

Java موجه الأعمال (II)

Java موجه الأعمال (III)

معالجة الاستثناء لـ Java

قائمة Java (List)

Java Queue (الصف)

مجموعات Java Map

مجموعات Java Set

تدفق البيانات في Java (I/O)

قراء/كتابة Java

مواضيع أخرى في Java

كلاس Java InputStreamReader

في هذا الدليل، سنستخدم الأمثلة لدراسة كلاس Java InputStreamReader وطرقه.

كلاس InputStreamReader في حزمة java.io يمكن استخدامها لتحويل بيانات البايت إلى بيانات الحروف.

It inherits the abstract class Reader.

The InputStreamReader class works with other input streams. It is also known as a bridge between byte streams and character streams. This is because InputStreamReader reads bytes as characters from the input stream.

For example, some characters need 2 bytes to store in memory. To read such data, we can use an input stream reader that reads 2 bytes together and converts it to the corresponding character.

Create an InputStreamReader

To create an InputStreamReader, we must first import the java.io.InputStreamReader package. After importing the package, we can create an input stream reader.

//Create an InputStream
FileInputStream file = new FileInputStream(String path);
//Create an InputStreamReader
InputStreamReader input = new InputStreamReader(file);

In the above example, we created an InputStreamReader named input and a FileInputStream named file.

Here, the data in the file is stored using some default character encoding.

However, we can also specify the type of character encoding in the file (UTF8orUTF16)

//Create an InputStreamReader, specify character encoding
InputStreamReader input = new InputStreamReader(file, Charset cs);

Here, we used the Charset class to specify the character encoding of the file.

Methods of InputStreamReader

The InputStreamReader class provides different implementations of the methods existing in the Reader class.

read() method

  • read() - Reads a single character from the reader

  • read(char[] array) - Reads characters from the reader and stores them in the specified array

  • read(char[] array, int start, int length) - Reads a character array of length equal to length from the reader and stores it in the specified array starting at start

For example, suppose we have a file namedinput.txtThe file contains the following content.

هذا هو سطر نصي داخل الملف.

Let's try to use the InputStreamReader to read this file.

import java.io.InputStreamReader;
import java.io.FileInputStream;
class Main {
  public static void main(String[] args) {
    //Create a character array
    char[] array = new char[100];
    try {
      //Create a FileInputStream
      FileInputStream file = new FileInputStream("input.txt");
      //Create an InputStreamReader
      InputStreamReader input = new InputStreamReader(file);
      //قراءة الأحرف من الملف
      input.read(array);
      System.out.println("بيانات الدفق:");
      System.out.println(array);
      //اغلاق reader
      input.close();
    }
    catch(Exception e) {}}
      e.getStackTrace();
    }
  }
}

Output Result

بيانات الدفق:
هذا هو سطر نصي داخل الملف.

في المثال السابق، استخدمنا دفق الإدخال لإنشاء قارئ الدفق الإدخالي. قارئ الدفق الإدخالي مرتبط بالملفinput.txtالروابط.

 FileInputStream file = new FileInputStream("input.txt");
 InputStreamReader input = new InputStreamReader(file);

لقراءة الأحرف من الملف، نستخدم طريقة read().

طريقة getEncoding()

يمكن استخدام طريقة getEncoding() للحصول على نوع رمز الترميز المستخدم لتحفظ البيانات في الدفق الإدخالي. على سبيل المثال،

import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.io.FileInputStream;
class Main {
  public static void main(String[] args) {
    try {
      //إنشاء FileInputStream
      FileInputStream file = new FileInputStream("input.txt");
      //استخدام الترميز الافتراضي لإنشاء InputStreamReader
      InputStreamReader input1 = new InputStreamReader(file);
      //إنشاء InputStreamReader بترميزُ مُحدد
      InputStreamReader input2 = new InputStreamReader(file, Charset.forName("UTF8"));
      //إرجاع رمز الترميز للدفق الإدخالي
      System.out.println("ترميز النصوص للـinput1: " + input1.getEncoding());
      System.out.println("ترميز النصوص للـinput2: " + input2.getEncoding());
      //اغلاق reader
      input1.close();
      input2.close();
    }
    catch(Exception e) {}}
      e.getStackTrace();
    }
  }
}

Output Result

Character encoding of input1: Cp1252
Character encoding of input2: UTF8

In the above example, we created two input stream readers, named input1 and input2.

  • input1 does not specify character encoding. Therefore, the getEncoding() method returns the standard name of the default character encoding.

  • input2 specifies character encodingUTF8. Therefore, the getEncoding() method returns the specified character encoding.

Note: We have used the Charset.forName() method to specify the type of character encoding.

close() method

To close the input stream reader, we can use the close() method. After calling this close() method, we will no longer be able to use the reader to read data.

Other methods of InputStreamReader

MethodDescription
ready()Check if the stream is ready to be read
mark()Mark the position of the data read in the stream
reset()Reset Markers