java 获取当前项目路径并且读取文件

package com.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.servlet.http.HttpServletRequest;

import com.mysql.jdbc.log.LogFactory;

/**
 * 读取本地文本文件
 * 
 * @author yushen
 * 
 */
public class StringOrbdfile {
	/**
	 * 
	 * @param fullFileName
	 * @return
	 */
	public static String getBDJson(String fullFileName) {
		File file = new File(fullFileName);
		Scanner scanner = null;
		StringBuilder buffer = new StringBuilder();
		try {
			scanner = new Scanner(file, "GBK");
			while (scanner.hasNextLine()) {
				buffer.append(scanner.nextLine());
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
		return buffer.toString();
	}

	/**
	 * ready Go
	 * 
	 */
	public static void main(String[] args) {
		// 获取当前项目路径
		String path = System.getProperty("user.dir");

		System.out.println(getBDJson(path + "/WebRoot/json/lol.json"));

	}
}

猜你喜欢

转载自blog.csdn.net/weixin_42749765/article/details/81533804