Skip to content

Instantly share code, notes, and snippets.

@satokjp
Last active December 25, 2015 18:19
Show Gist options
  • Save satokjp/7019473 to your computer and use it in GitHub Desktop.
Save satokjp/7019473 to your computer and use it in GitHub Desktop.
java test program
//
// test.java
// Compile: javac test.java
// Run: java test -t/-b [file name]
//
// env LC_ALL=en javac test.java :mac
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
class test
{
// main
public static void main( String[] argv ){
System.out.println("test program for Java");
if (argv.length !=2){
System.out.println("usage: java test -t/-b [file name]");
System.exit(1);
}
String s=argv[0];
if( s.equals("-t") ) {
// -t スイッチでの動作
System.out.println("text file");
// text file write
fileWrite_txt(argv[1]);
// text file read
fileRead_txt(argv[1]);
}else if( s.equals("-b") ) {
// -s スイッチでの動作
System.out.println("bin file");
// bin file write
try{
fileWrite_bin(argv[1]);
}catch(IOException e){
System.out.println(e);
}
// bin file read
try{
fileRead_bin(argv[1]);
}catch(IOException e){
System.out.println(e);
}
// bin test
test_bin();
}else {
System.out.println("no such switch");
}
}
// txt file write
private static void fileWrite_txt( String file_w ){
Date date1 = new Date();
try{
System.out.println("Write: "+file_w);
File file = new File("test.txt");
if (checkBeforeWritefile(file)){
FileWriter filewriter = new FileWriter(file);
filewriter.write( date1 + "\r\n");
filewriter.write("hello\r\n");
filewriter.write("how are you\r\n");
filewriter.close();
}else{
System.out.println("can not write");
}
}catch(IOException e){
System.out.println(e);
}
}
//
private static boolean checkBeforeWritefile(File file){
if (file.exists()){
if (file.isFile() && file.canWrite()){
return true;
}
}
return false;
}
// text file read
private static void fileRead_txt( String file_r ){
try{
System.out.println("Read: "+file_r);
File file = new File("test.txt");
FileReader filereader = new FileReader(file);
int ch = filereader.read();
while(ch != -1){
System.out.print((char)ch);
ch = filereader.read();
}
}catch(FileNotFoundException e){
System.out.println(e);
}catch(IOException e){
System.out.println(e);
}
}
// binary file write
private static void fileWrite_bin( String file_w ) throws IOException {
System.out.println("Write_bin: "+file_w);
FileOutputStream out = new FileOutputStream("test.bin");
try {
// 順番にバイナリデータを書き込む
out.write(0x12);
out.write(0x34);
out.write(0x56);
out.write(0x78);
out.write(0x9a);
out.write(0xbc);
out.write(0xde);
out.write(0xf0);
}
finally {
// try-finally で確実にファイルを閉じる (FileWriter の時と同じ)
out.close();
}
}
// binary file read
private static void fileRead_bin( String file_r ) throws IOException {
System.out.println("Read_bin: "+file_r);
FileInputStream in = new FileInputStream("test.bin");
try {
while (true) {
// 1 バイトのデータを読み込む
int b = in.read();
// FileReader 同様、ファイルの終端では -1 が返る
if (b == -1) {
break;
}
// print (bを16進表記したもの) + " "
// Integer.toHexString(int) で16進表記に変換できる
System.out.print(Integer.toHexString(b) + " ");
}
}
finally {
in.close();
}
}
// test bin
private static void test_bin(){
byte[] b = {(byte)0x95,(byte)0x96};
System.out.println();
System.out.println("test_bin");
System.out.printf("%02x", b[0]);
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment