Monthly Archives: 1月 2012

「Java入門」java.util.Arrays.toString(binarySearch)の使い方

Javaコード
package com.arkgame.study;
import java.util.Arrays;

public class ChangfaTuDemo {

public static void main(String[] args) {

int cft[] = { 13, 24, 35, 47, 69, 16, 62, 76, 98 };
Arrays.sort(cft);
System.out.println(“after sort:”);
System.out.println(Arrays.toString(cft));
int point = Arrays.binarySearch(cft, 47);
System.out.println(“find element 47 position:” + point);
Arrays.fill(cft, 99);
System.out.println(“fill element:”);
System.out.println(Arrays.toString(cft));

}

}

結果
after sort:
[13, 16, 24, 35, 47, 62, 69, 76, 98]
find element 47 position:4
fill element:
[99, 99, 99, 99, 99, 99, 99, 99, 99]

java.lang.String.getBytes()でファイルを書き込むサンプル

Javaコード
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BytesFileDemo {

public static void main(String[] args) {
String fileName = “C:\\arkgame” + File.separator + “hello.txt”;
File f = new File(fileName);

try {
OutputStream out = new FileOutputStream(f);

String str = “this is a test”;
byte[] b = str.getBytes();
for (int i = 0; i < b.length; i++) {
out.write(b[i]);
}
out.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

}

}