반응형

 

byte[] to String

 byte[] ={'a','b',};
 String str = new String(data, StandardCharsets.UTF_8);
 
 
 
 byte[] ={'a','b',};
 String str = new String(data);

 

String.toCharArray()

 

String str = "abcdefg";
char [] array = str.toCharArray();

 

 

String.getBytes();

String str= "Hello";
byte buff[] = str.getBytes();
//charset
byte buff1[] = str.getBytes(StandardCharsets.UTF_8);

//charset string
try {
	byte buff2[] = str.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
	e.printStackTrace();
}

 

byte[] to String

String str = "Hello";
byte buff[] = str.getBytes();
String str1 = new String(buff);

 

 

InputStream -> ByteArrayOutputStream -> String

(주로 File IO, Asset , network등 이용하는 경우 많이 사용하게 됨.)

 

InputStream input = this.getContext().getAssets().open("mytextfile.txt");

try {
	ByteArrayOutputStream result = new ByteArrayOutputStream();

	byte buff[] = new byte[1024];
	int length;
	while( (length = input.read(buff,0, 1024)) != -1){
		result.write(buff,0,length);
	}
	String str = result.toString("UTF-8");

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

 

 

 

'Android, Java,Kotlin' 카테고리의 다른 글

JNI, Native code build 시 유의점  (0) 2022.04.21
[Copy&Paste] Array를 List로 바꾸기  (0) 2020.11.04
[Android] JavaDoc 사용법 링크.  (0) 2020.10.28
반응형

유용하면서 간단한 디자인패턴 : 싱글톤 패턴

 

Singleton class  Basic type

 

 

public class Singleton {
	private volatile static Singleton uniqueInstance = null;
	
	private Singleton() {}
	public static Singleton getInstance (){
		if ( uniqueInstance == null){
			synchronized( Singleton.class){
				if (uniqueInstance == null)
					uniqueInstance = new Singleton();
			}
		}
		return uniqueInstance;
	}
}

 

 

 

'개발 Note > Codes' 카테고리의 다른 글

[Andriod] Timer and TimerTask  (0) 2020.10.22
C++ object 관리를 위한 ObjectRef 구조 설계  (0) 2015.06.26
sorting algorithms  (0) 2014.02.26
c++ while 문 - 잘 안쓰는 표현  (0) 2013.10.23
UML2 Sementics  (0) 2012.01.11

+ Recent posts