Java で文字列を InputStream に変換する
-
Java で文字列を
InputStreamに変換するにはByteArrayInputStream()を用いる -
Java で
StringReaderとReaderInputStreamを用いて文字列をInputStreamに変換する -
文字列を
InputStreamに変換するにはorg.apache.commons.io.IOUtilsを使用する
Java で文字列を InputStream に変換する方法について、いくつかのメソッドを用いて説明します。文字列は文字の集合であり、InputStream はバイトの集合です。Java で文字列を InputStream に変換する方法を見てみましょう。
Java で文字列を InputStream に変換するには ByteArrayInputStream() を用いる
Java の Input/Output パッケージには、バイト配列を InputStream として読み込むクラス ByteArrayInputStream があります。まず、getBytes() を用いて exampleString から UTF_8 の文字セットを持つバイトを取得し、それを ByteArrayInputStream に渡します。
目的を達成できたかどうかを確認するために、read() を使って inputStream を読み込み、バイトごとに char に変換します。これは元の文字列を返します。
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws IOException {
String exampleString = "This is a sample string";
InputStream inputStream =
new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));
// To check if we can read the string back from the inputstream
int i;
while ((i = inputStream.read()) != -1) {
char getSingleChar = (char) i;
System.out.print(getSingleChar);
}
}
}
出力:
This is a sample string
Java で StringReader と ReaderInputStream を用いて文字列を InputStream に変換する
文字列を InputStream に変換する 2つ目の手法は、StringReader と ReaderInputStream の 2つのメソッドを用います。前者は文字列を読み込んで reader にラップするために使われ、後者は reader と charsets の 2つの引数を取ります。最後に、InputStream を取得します。
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.input.ReaderInputStream;
public class Main {
public static void main(String[] args) throws IOException {
String exampleString = "This is a sample string";
StringReader stringReader = new StringReader(exampleString);
InputStream inputStream = new ReaderInputStream(stringReader, StandardCharsets.UTF_8);
// To check if we can read the string back from the inputstream
int i;
while ((i = inputStream.read()) != -1) {
char getSingleChar = (char) i;
System.out.print(getSingleChar);
}
}
}
出力:
This is a sample string
文字列を InputStream に変換するには org.apache.commons.io.IOUtils を使用する
タスクを簡単にするために Apache Commons ライブラリを使用することもできます。この Apache Commons ライブラリの IOUtls クラスには、文字列と使用する文字セットを受け取る toInputStream() メソッドがあります。このメソッドは Java の文字列を InputStream に変換するために 1つのメソッドを呼び出すだけなので、最も簡単です。
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
public class Main {
public static void main(String[] args) throws IOException {
String exampleString = "This is a sample string";
InputStream is = IOUtils.toInputStream(exampleString, StandardCharsets.UTF_8);
// To check if we can read the string back from the inputstream
int i;
while ((i = is.read()) != -1) {
char getSingleChar = (char) i;
System.out.print(getSingleChar);
}
}
}
出力:
This is a sample string
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedIn関連記事 - Java String
- Java で 16 進文字列のバイト配列を変換する方法
- Java で文字列から部分文字列を削除する方法
- Java で文字列から文字列配列への変換を実行する方法
- Java 文字列をバイトに変換する方法
- Java でランダムな文字列を生成する
- Java のスワップメソッド
