자바에서 링크로 파일을 다운로드 받아야 하는 경우가 있다. 

그럴경우 간단한 방법이 있다.

 
 
try{
	java.net.URL downLink = new java.net.URL("다운로드링크 지정-input downloadLink(String type)");
	java.nio.channels.ReadableByteChannel rbc = java.nio.channels.Channels.newChannel(downLink.openStream());
	java.io.FileOutputStream outputStream = new java.io.FileOutputStream("파일명.확장자(ex.fileName.txt)");
	outputStream.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
				
}catch(java.io.IOException e){
		e.printStackTrace();
}




posted by Culinary developer

AND



Iterable<> 타입을 List<> 타입으로 형변환 하는 법은 굳이 반복문을 돌리지 않아도 되는 두가지 방법이 있다. 


 첫번째 방법
import com.google.common.collect.Lists;



Iterator<> myIterator = ... //some iterator

List<> myList = Lists.newArrayList(myIterator);


두번째 방법 
import org.apache.commons.collections.IteratorUtils;



Iterator<> myIterator = ...//some iterator

List<> myList = IteratorUtils.toList(myIterator); 


posted by Culinary developer



AND


이번에는 해당 위치에 폴더가 없을 경우 지정해준 디렉토리에 맞게 


자동으로 폴더를 생성해주는 exists() 메서드를 포스팅 하겠습니다.


예제의 경우엔 C드라이브 하위에 Test란 폴더를 미리 생성해 두고 하위로 file이란 폴더를 생성하는 방식으로 


진행하도록 하겠습니다.



그림을 보시면 Test란 폴더의 하위엔 아무런 폴더가 없습니다.



*code

import java.io.File;

public class CreateFileDirectoryTest {

	//메인 메서드 실행
	public static void main(String[] args) {
		
		//생성할 파일경로 지정
		String path = "c://test//file.txt";
		//파일 객체 생성
		File file = new File(path);
		//!표를 붙여주어 파일이 존재하지 않는 경우의 조건을 걸어줌
		if(!file.exists()){
			//디렉토리 생성 메서드
			file.mkdirs();
			System.out.println("created directory successfully!");
		}
	}
}
}



메서드를 실행시키니 성공적으로 디렉토리가 생성되었다는 문자열이 콘솔창에 찍히는 것을 확인 할 수 있습니다.


*result



다시 Test파일을 보니 file이란 폴더가 생성된 것을 확인하실 수 있습니다.


이상으로 폴더 디렉토리 생성에 관한 포스팅을 마치도록 하겠습니다.

Posted by Culinary developer


AND

이번 포스팅은 초간단 split 메서드를 이용해서 string 문자열을 잘라 배열에 담는법을 소개하려고 합니다. 


split메서드는 split("")에서 큰따옴표("")안의 문자를 기준으로 문자열을 잘라서 배열에 담아주는 녀석으로 

예를들어 "다음,네이버,구글,인터넷"이라는 문자열이 있을때 ,를 기준으로 나누고 싶다면 

문자열.split(",")의 방식으로 지정해 주면 됩니다. 샘플코드 및 결과 값은 아래와 같습니다. 


 *샘플코드 (sample code)
package com.ujin.test;

import java.util.Arrays;

public class StringTest {

	//메인 메서드로 실행
	public static void main(String[] args) {
		
		StringTest st = new StringTest();
		st.splitTest();
		
	}
	
	
	//split메서드를 이용해서 스트링 문자열 자르기 sample	 
	public void splitTest(){
		
		//String 문자열 생성
		String testStr = "다음,네이버,구글,인터넷";
		
		//split 메서드안에 잘라줄 기준이 되는 문자 입력
		String[] testArr = testStr.split(",");
		
		//결과 값 콘솔출력
		System.out.println(Arrays.deepToString(testArr));
	}
}


*콘솔 결과창 (console result)


이상으로 포스팅을 마치겠습니다.


Posted by Culinary developer





AND