Javaを使用してプリミティブデータをラッパークラスに変換するにはどうすればよいですか?
Javaは、java.langパッケージでラッパークラスと呼ばれる特定のクラスを提供します。これらのクラスのオブジェクトは、それらの中にプリミティブデータ型をラップします。
ラッパークラスを使用すると、ArrayList、HashMapなどのさまざまなCollectionオブジェクトにプリミティブデータ型を追加することもできます。ラッパークラスを使用して、ネットワーク経由でプリミティブ値を渡すこともできます。
例
import java.util.Scanner;
public class WrapperExample {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer value: ");
int i = sc.nextInt();
//Wrapper class of an integer
Integer obj = new Integer(i);
//Converting Integer object to String
String str = obj.toString();
System.out.println(str);
//Comparing with other object
int result = obj.compareTo(new Integer("124"));
if(result==0) {
System.out.println("Both are equal");
}else{
System.out.println("Both are not equal");
}
}
} 出力
Enter an integer value: 1211 1211 Both are not equalHow to create and use directories in Java?
ディレクトリの作成
ファイルクラスと同様に、java.nioパッケージのFilesクラスは createTempFile()を提供します のプレフィックスとサフィックスを表す2つの文字列パラメータを受け入れ、指定された詳細を含む一時ファイルを作成するメソッド。
createDirectory () ファイルのメソッド クラスは必要なディレクトリのパスを受け入れ、新しいディレクトリを作成します。
例
次の例では、FilesクラスのcreateDirectory()メソッドを使用して新しいディレクトリを作成します。
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Test {
public static void main(String args[]) throws IOException {
//Creating a path object
String pathStr = "D:\\sample_directory ";
Path path = Paths.get(pathStr);
//Creating a directory
Files.createDirectory(path);
System.out.println("Directory created successfully");
}
} 出力
Directory created successfully
確認すると、作成されたディレクトリが-
として表示されます。
ディレクトリの内容を一覧表示する
newDirectoryStream() ファイルのメソッド classは、指定されたパスでディレクトリを開き、ディレクトリの内容を提供するディレクトリストリームを返します。
例
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FilesExample {
public static void main(String args[]) throws IOException {
//Creating a path object
String pathStr = "D:\\ExampleDirectory";
Path path = Paths.get(pathStr);
System.out.println("Contents off the specified directory");
DirectoryStream stream = Files.newDirectoryStream(path);
for (Path file: stream) {
System.out.println(file.getFileName());
}
}
} 出力
Contents off the specified directory demo1.pdf demo2.pdf sample directory1 sample directory2 sample directory3 sample directory4 sample_jpeg1.jpg sample_jpeg2.jpg test test1.docx test2.docx
ディレクトリフィルターの使用
DirectoryStream.Filterを使用してディレクトリをフィルタリングできます。次の例では、指定したパスのディレクトリをフィルタリングします。
例
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Test {
public static void main(String args[]) throws IOException {
//Creating a path object
String pathStr = "D:\\ExampleDirectory";
Path path = Paths.get(pathStr);
System.out.println("Directories in the specified directory");
DirectoryStream.Filter filter = new DirectoryStream.Filter(){
public boolean accept(Path file) throws IOException {
return (Files.isDirectory(file));
}
};
DirectoryStream list = Files.newDirectoryStream(path, filter);
for (Path file : list) {
System.out.println(file.getFileName());
}
}
} 出力
Directories in the specified directory hidden directory1 hidden directory2 sample directory1
-
Javaを使用してプリミティブデータをラッパークラスに変換するにはどうすればよいですか?
Javaは、java.langパッケージでラッパークラスと呼ばれる特定のクラスを提供します。これらのクラスのオブジェクトは、それらの中にプリミティブデータ型をラップします。 ラッパークラスを使用すると、ArrayList、HashMapなどのさまざまなCollectionオブジェクトにプリミティブデータ型を追加することもできます。ラッパークラスを使用して、ネットワーク経由でプリミティブ値を渡すこともできます。 例 import java.util.Scanner; public class WrapperExample { public static void ma
-
Java OpenCVライブラリを使用してネガティブイメージをポジティブイメージに変換するにはどうすればよいですか?
ネガティブイメージをポジティブに変換するには- ImageIO.read()メソッドを使用して必要な画像を読み取ります。 画像の高さと幅を取得します。 ネストされたforループを使用すると、画像の各ピクセルをトラバースします。 getRGB()メソッドを使用してピクセル値を取得します。 ピクセルから各値を取得するには、各色の開始位置に右シフトする必要があります。つまり、赤の場合はアルファ16の場合は24、赤の場合はビット単位で操作し、0Xffで操作します。これにより、変数がマスクされ、最後の8ビットが残り、残りのビットはすべて無視されます。 新しい赤、緑、青