Java File

//파일 바이너리 저장
	// int to byte
	byte[] bb = new byte[Integer.BYTES];
	//ByteBuffer buf = ByteBuffer.wrap(bb);
	//buf.putInt(x);
	bb[0] = (byte)(x>>24);
	bb[1] = (byte)(x>>16);
	bb[2] = (byte)(x>>8);
	bb[3] = (byte)x;
	FileOutputStream fos = new FileOutputStream("004.txt");
	BufferedOutputStream bos = new BufferedOutputStream(fos);
	bos.write(bb);
	bos.close();
//파일 바이너리 불러오기
	FileInputStream fis = new FileInputStream("001.txt");
	BufferedInputStream bis = new BufferedInputStream(fis);
	byte[] bb = new byte[Integer.BYTES];
	bis.read(bb);
	 int x = 0;
	 x |= (bb[0] & 0xff) <<24;
	 x |= (bb[1] & 0xff) <<16;
	 x |= (bb[2] & 0xff) <<8;
	 x |=( bb[3] & 0xff);
	return x;
//파일 존재
	File f = new File("001.txt");
	System.out.println(f.exists()); //exists
//파일 삭제
	f.delete();
//파일 삭제 프로그램 종료 시
	f.deleteOnExit();
//임시파일
	File ft = File.createTempFile("test",".myfile");
//파일 경로
	System.out.println(ft.getAbsolutePath());

Leave a Comment