java.io.FileOutputStream.FileOutputStream()の使い方

サンプルコード
package com.arkgame.study;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FileOutputStreamDemo {

public static void main(String[] args) {
String fileName = “D:\\soft” + File.separator + “world.txt”;
File f = new File(fileName);
OutputStream out;
try {
out = new FileOutputStream(f);
String str = “test data”;
byte[] b = str.getBytes();
out.write(b);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

}

}

「javaScript入門」for文で配列内の文字列を検索する方法

JSコード
var empArr = [‘山田’, ‘杉浦’, ‘大崎’, ‘品川’, ‘上野’,’井上’];

var empName = ‘品川’;
var flag = false;
for (var i = 0; i < empArr.length; i++) {
if(empArr[i] === empName) {
flag = true;
break;
}
}
console.log(‘結果:’, flag);
//alert(“resutlt:”+flag);

「Java入門」java.util.StringTokenizerの使い方

Javaコード
import java.util.StringTokenizer;

public class TokenizerDemo {

public static void main(String[] args) {
String dig = “Tokyo/Fukuoka/Oosaka/Okinawa”;
String delim = “/”;
StringTokenizer st = new StringTokenizer(dig, delim);
int count = st.countTokens();
String[] cft = new String[count];
for (int i = 0; i < count; i++) {
cft[i] = st.nextToken();
System.out.println(cft[i]);
}
}

}

結果
Tokyo
Fukuoka
Oosaka
Okinawa

「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();
}

}

}