java 文件读写操作

	public String fileReader(String dataDirPath){
		FileReader fr = null;
		LineNumberReader lnr = null;
		String result = "";
		try {
			if(dataDirPath!=null&&!"".equals(dataDirPath)){
				File dataFile = new File(dataDirPath);
				if(dataFile.exists()){
					fr = new FileReader(dataFile);
					lnr = new LineNumberReader(fr);
					String line = null;
					//循环读取信息
					while((line = lnr.readLine())!=null){
						String[] value = line.split("     ");
						result = result+value[0];
					}
					lnr.close();
					fr.close();
				}
			}
		} catch (Exception e) {
		}
		return result;
	}
	/**
	 * @param filePath 文件路径
	 * @param fileName 文件名
	 * @param stationList 要写入的数据
	 */
	public void fileWrite(String filePath,String fileName,List<String> stationList){
		FileWriter fw = null;
		try {
			String line = null;
			boolean isFirst = true;
			for(String station:stationList){
				if(isFirst){
					line = station;
					isFirst = false;
				} else {
					line = line + "," + station;
				}
			}
			fw = new FileWriter(filePath + fileName + ".txt", true);
			fw.write(line + "\r\n");
			fw.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(fw!=null){
					fw.close();
				}
			} catch (IOException e2) {
			}
		}
	}

猜你喜欢

转载自963084302.iteye.com/blog/2246793