'Read'에 해당되는 글 4건

  1. 2022.03.16 [Java] Read Properties File.
  2. 2020.07.21 [Java] jar 파일 내 resources 읽기 - ClassPathResource
  3. 2020.04.07 [Shell] Bash read, if then fi
  4. 2013.02.01 C++ file read/write as buffer size

[Java] Read Properties File.

ITWeb/개발일반 2022. 3. 16. 15:19

일반 Java Application 프로젝트에서 resources 폴더 아래 application.properties 같은 정보를 가져와야 할 때가 있습니다.

아래 처럼 사용 하시면 됩니다.

 

application.properties)

host=localhost
port=8888

 

MainApplication.java)

public void readProperties() {
    Properties prop = new Properties();
    
    try ( InputStream stream = MainApplication.class.getClassLoader().getResourceAsStream("application"
    +".properties")) {
    	prop.load(Stream);
    } catch (Exception e) {
    }
    
    try {
    	Resource resource = new ClassPathResource("application.properties");
        prop.load(resource.getInputStream());
    } catch (Exception e) {
    }
    
    System.out.println( prop.getProperty("host") );
    System.out.println( prop.getProperty("port") );
}

 

:

[Java] jar 파일 내 resources 읽기 - ClassPathResource

ITWeb/개발일반 2020. 7. 21. 20:23

제목에 있는 그대로 입니다.

 

[참고문서]

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/ClassPathResource.html

 

ClassPathResource 를 이용해서 구현 하면 됩니다.

// resourceFile 은 resources 를 제외한 path 와 filename 을 작성 합니다.

ClassPathResource resource = new ClassPathResource(resourceFile);

 

아래 예제 코드는 programcreek.com 에서 가져 왔습니다.

public static String getResourceAsString(String path) {
	try {
		Resource resource = new ClassPathResource(path);
		try (BufferedReader br = new BufferedReader(
				new InputStreamReader(resource.getInputStream()))) {
			StringBuilder builder = new StringBuilder();
			String line;
			while ((line = br.readLine()) != null) {
				builder.append(line).append('\n');
			}
			return builder.toString();
		}
	}
	catch (IOException e) {
		throw new IllegalStateException(e);
	}
}

이와 같이 구현을 한 이유는 로컬에서는 잘 되는데 jar 로 말았을 때는 file 을 찾지 못하는 에러가 발생을 해서 입니다.

:

[Shell] Bash read, if then fi

ITWeb/개발일반 2020. 4. 7. 20:07
#!/usr/bin/env bash

pwd

echo "Are you sure current directory? (Y/N)"
read answer

if [ "$answer" == "y" ] || [ "$answer" == "Y" ]
then
  echo "Yes!!"
fi

echo "done!!"
#!/usr/bin/env bash

pwd

echo "Are you sure current directory? (Y/N)"
read ans
answer=`echo $ans| tr a-z A-Z`

if [ "$answer" == "Y" ]
then
  echo "Yes!!"
fi

echo "done!!"

 

:

C++ file read/write as buffer size

ITWeb/개발일반 2013. 2. 1. 14:55

file read and write as block size.


#include <iostream>
#include <fstream>
using namespace std;

const int32_t BUFFER_SIZE = 64;

int main () {
        char* buff;
        int32_t currSize = 0;
        int32_t totalSize = 0;
        int32_t extraSize = 0;

        ifstream is;
        ofstream os;

        is.open ("plain.txt", ifstream::in);
        os.open ("plain_bak.txt", ofstream::ate);

        // get size of file:
        is.seekg (0, ios::end);
        totalSize = is.tellg();
        is.seekg (0, ios::beg);

        cout << "file size : " << totalSize << endl;

        // allocate memory:
        buff = new char[BUFFER_SIZE];

        while ( !is.eof()  ) {
                extraSize = totalSize - currSize;

                if ( extraSize >= BUFFER_SIZE ) {
                        memset(buff, 0x00, BUFFER_SIZE);
                        is.read(buff, BUFFER_SIZE);
                        os.write(buff, BUFFER_SIZE);
                } else {
                        memset(buff, 0x00, BUFFER_SIZE);
                        is.read(buff, extraSize);
                        os.write(buff, extraSize);
                }

                currSize = currSize + BUFFER_SIZE;
        }

        is.close();
        os.close();

        delete[] buff;
        return 0;
}


: