'자바 Java'에 해당되는 글 15건

  1. 2016.11.22 WriteRecentCoordinate
  2. 2015.09.22 ReadRecentCoordinate
  3. 2014.12.24 오늘은 무슨 요일
  4. 2014.12.05 Thread interrupt
  5. 2014.11.26 입력 문자열에서 숫자를 뽑아 문자로 반환
자바 Java2016. 11. 22. 19:08










텍스트 파일에 자료값을 쓰는 내용.

매번 프로젝트를 열어 해당 코드를 가져와서 사용하는 불편함을 줄이려고한다

블로그에 올려두고 필요할 때 복사해서 붙여넣고

상황에 맞추어 수정해서 사용할 예정이다.

앞으로 이와 같은 내용은 많이 쓰일 듯하다.






import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;



public class WriteRecentCoordinate {

private boolean writeWords(String sTextfile, StringBuilder sb) { // "f:\\AaaText.txt"

File file = new File(sTextfile);

BufferedWriter bw = null;

try{

            bw = new BufferedWriter(new FileWriter(file, false));

            

            bw.write(sb.toString());

            bw.flush();             

        } catch(Exception e){

            e.printStackTrace();

        } finally {

System.out.println("\n\n\nfinally {");

        try {

        if(bw != null)

        bw.close();

} catch (IOException e) {

e.printStackTrace();

System.out.println("} <<-- finally\n\n\n");

        }

return true;

}

public static void main(String[] args) {

WriteRecentCoordinate doSomething = new WriteRecentCoordinate();

StringBuilder sb = new StringBuilder() // "\r\n": window,  "\n": linus

.append("target ").append("true").append("\r\n")

.append("tarLatitude ").append("37.26389").append("\r\n")

.append("tarLongitude ").append("127.02861").append("\r\n")

.append("\r\n")

.append("radius ").append("450f").append("\r\n")

.append("alarmSound ").append("true").append("\r\n")

.append("alarmVibration ").append("true").append("\r\n")

.append("alarmNumber ").append("55").append("\r\n")

.append("alarmRingtone ").append("null").append("\r\n")

.append("followCurrent ").append("0");

if(doSomething.writeWords("f:\\AaaText.txt", sb) )

System.out.println("Process Done");

else

System.out.println("Process failed!");

}

}




.

'자바 Java' 카테고리의 다른 글

ReadRecentCoordinate  (0) 2015.09.22
오늘은 무슨 요일  (0) 2014.12.24
Thread interrupt  (0) 2014.12.05
입력 문자열에서 숫자를 뽑아 문자로 반환  (0) 2014.11.26
자바 환경 변수 설정 CLASSPATH, JAVA_HOME, Path  (0) 2014.07.24
Posted by 코드버무려
자바 Java2015. 9. 22. 23:39







안드로이드 앱에 사용한 소스이다.

원하는 기본 기능을 하는 아래 코드를 먼저 자바로 만들었다.

안드로이드에 가져가서 적절하게 더하고 붙였다.





비슷한 기능을 다른 프로젝트에서도 자주 사용하리라 생각든다.

때마다 이전 프로젝트를 열어 원하는 코드를 찾아오는 많은 시간소모가 있었다.

이렇게 포스트 해놓으면 차후에 더 빠르고 효율적으로 사용하고자 한다






import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;



public class ReadRecentCoordinate {

public static final String FIRST_Line_WORD_Question = "target";

public static final String FIRST_Line_WORD_Answer = "true";

public static final String DELIMITEer = " "; // " " is one space. Not tab("\t").


private boolean readWords(String sTextfile) { // "f:\\AaaText.txt"

File file = new File(sTextfile);

BufferedReader br = null;


try {

br = new BufferedReader(new FileReader(file));


String line = null;

String[] splitedStr = null;


line = br.readLine();

if(line != null ){

splitedStr = line.split(DELIMITEer);

if(splitedStr.length < 2)

return false;

else {

if(!splitedStr[0].equals(FIRST_Line_WORD_Question) )

return false;

if(!splitedStr[1].equals(FIRST_Line_WORD_Answer))

return false;

printMiddle(splitedStr);

} else {

return false;

}

           

while( (line = br.readLine()) != null ) {

if(line.startsWith("//"))

continue;

 

splitedStr = null;

splitedStr = line.split(DELIMITEer);

 

printMiddle(splitedStr);

}

} catch (FileNotFoundException fnf) {

fnf.printStackTrace();

} catch( IOException e) {

e.printStackTrace();

} finally {

System.out.println("\n\n\nfinally {");

try {

        if(br != null)

        br.close();

} catch (IOException e) {

e.printStackTrace();

}

System.out.println("} <<-- finally\n\n\n");

}

return true;

}

private void printMiddle(String[] splitedStr) {

for (int i = 0; i < splitedStr.length; i++) {

splitedStr[i] = splitedStr[i].trim();

if(i < splitedStr.length -1)

System.out.print(splitedStr[i] +" ");

else 

System.out.println(splitedStr[i]);

}

}

public static void main(String[] args) {

ReadRecentCoordinate doSomething = new ReadRecentCoordinate();

if(doSomething.readWords("f:\\AaaText.txt") )

System.out.println("Process Done");

else

System.out.println("Process failed!");

}

}


'자바 Java' 카테고리의 다른 글

WriteRecentCoordinate  (0) 2016.11.22
오늘은 무슨 요일  (0) 2014.12.24
Thread interrupt  (0) 2014.12.05
입력 문자열에서 숫자를 뽑아 문자로 반환  (0) 2014.11.26
자바 환경 변수 설정 CLASSPATH, JAVA_HOME, Path  (0) 2014.07.24
Posted by 코드버무려
자바 Java2014. 12. 24. 20:28






















String getWhatToday() {

Calendar cal = Calendar.getInstance( );

StringBuffer strBffr = new StringBuffer( );

int iDay = cal.get(Calendar.DAY_OF_WEEK);

switch(iDay) {

case 1:

strBffr.append("i"); break;

case 2:

strBffr.append("M"); break;

case 3:

strBffr.append("H"); break;

case 4:

strBffr.append("W"); break;

case 5:

strBffr.append("T"); break;

case 6:

strBffr.append("F"); break;

case 7:

strBffr.append("s"); break;

}

return strBffr.toString( );

}

'자바 Java' 카테고리의 다른 글

WriteRecentCoordinate  (0) 2016.11.22
ReadRecentCoordinate  (0) 2015.09.22
Thread interrupt  (0) 2014.12.05
입력 문자열에서 숫자를 뽑아 문자로 반환  (0) 2014.11.26
자바 환경 변수 설정 CLASSPATH, JAVA_HOME, Path  (0) 2014.07.24
Posted by 코드버무려
자바 Java2014. 12. 5. 00:04






















public class JavaExam {

public static void main(String[] args) {

System.out.println("Start");

Thread threadA = new MyThread("Thread AAA");

threadA.start();


try {

Thread.sleep(1500);

} catch (InterruptedException e) {

e.printStackTrace();

}

threadA.interrupt();


System.out.println("------  끝  ------");

}

}


class MyThread extends Thread {

MyThread(String name) {

setName(name);

}


public void run() {


try {

Thread.sleep(10000);

}

catch (InterruptedException e) {

System.out.println("   " +getName() + "InterruptedException예외 발생");

}

System.out.println("   " +getName() +"   끝");

}

}


*************************************************


class JavaExam {

public static void main(String[] args) {

        System.out.println("------- 시작 -------");

        Thread threadA = new MyThread("Thread AAA");

        threadA.start();

        

        try {

Thread.sleep(1500);

} catch (InterruptedException e) {

e.printStackTrace();

}

        

        threadA.interrupt();

        try {

threadA.join();

} catch (InterruptedException e) {

e.printStackTrace();

}

        

        System.out.println("------  끝  ------");

   

   }


}


class MyThread extends Thread {

MyThread(String name) {

setName(name);

}

    public void run() {

        

        try {

            Thread.sleep(10000);

        }

        catch (InterruptedException e) {

            System.out.println("   " +getName() + "InterruptedException예외 발생");

        }

        

        try {

Thread.sleep(10); // main 스레드 출력 순서 테스트

} catch (InterruptedException e) {

e.printStackTrace();

}

        System.out.println("    " +getName() +"   끝");

    }

}


**************************************************


class JavaExam {

public static void main(String[] args) {

        System.out.println("------- 시작 -------");

        MyThread threadA = new MyThread("Thread AAA");

        

        threadA.setRunning(true);

        threadA.start();

        

        try {

Thread.sleep(1500);

} catch (InterruptedException e) {

e.printStackTrace();

}

        

        threadA.interrupt();

        try {

threadA.join();

} catch (InterruptedException e) {

e.printStackTrace();

}

        

boolean done = true;

threadA.setRunning(false);

while (done) {

try {

threadA.join();

done = false;

} catch (InterruptedException e) {

e.printStackTrace();

}

}

        

        System.out.println("------  끝  ------");

   }

}


class MyThread extends Thread {

boolean isRun;

MyThread(String name) {

setName(name);

}

public void setRunning(boolean run) {

this.isRun = run;

}

    public void run() {

        

        try {

            Thread.sleep(10000);

        }

        catch (InterruptedException e) {

            System.out.println("   " +getName() + "InterruptedException예외 발생");

        }

        

        try {

Thread.sleep(10); // main 스레드 출력 순서 테스트

} catch (InterruptedException e) {

e.printStackTrace();

}

        System.out.println("   "  +getName() +"   끝");

    }

}

Posted by 코드버무려
자바 Java2014. 11. 26. 01:21




String extractNumber(String str)

 {

if (str == null)

return str;


StringBuffer sb = new StringBuffer();

int length = str.length();

for (int i = 0; i < length; i++) {

char curChar = str.charAt(i);

if (Character.isDigit(curChar))

sb.append(curChar);

//else return sb.toString();

else break;

}

return sb.toString();

 } 





'자바 Java' 카테고리의 다른 글

오늘은 무슨 요일  (0) 2014.12.24
Thread interrupt  (0) 2014.12.05
자바 환경 변수 설정 CLASSPATH, JAVA_HOME, Path  (0) 2014.07.24
구글 개발자 등록 과정  (0) 2014.07.24
프로젝트 구조  (0) 2014.07.23
Posted by 코드버무려