IO流读取写入文件,通过map遍历

public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
		URL url = ClassLoader.getSystemClassLoader().getResource("./");
		File file =new File(url.getPath());
		File parentFile =new File(file.getParent());
		
		File directory = new File("");//参数为空 
		String courseFile = directory.getCanonicalPath() ; 
		System.out.println(courseFile); 
		
		//源地址
		File fileStations = new File(parentFile.getParent() + "\\stations.txt");
		//写入文件地址
		File fileCopy = new File(parentFile.getParent() + "\\subway8.txt");
		
		BufferedReader br = null;
		BufferedWriter bw = null;  
		
		try {
			//如果文件不存在就创建
			if (!(fileCopy.exists())) {
				fileCopy.createNewFile();
			} 
			br = new BufferedReader(new FileReader(parentFile.getParent() + "\\stations.txt"));  
            bw = new BufferedWriter(new FileWriter(parentFile.getParent() + "\\subway8.txt")); 
            
            String str = "";  
            int i = 1;  
            while((str = br.readLine()) != null){  
                bw.write(i+"="+str);  
                bw.newLine();  
                i++;  
            }  
            bw.flush();  //此处很关键,如果不写该语句,是不能从缓冲区写到文件里的 
            bw.close();  
            br.close();
            
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
        }  
		
		
		String filename = parentFile.getParent() + "\\subway8.txt";
		Map<String, String> filemaps = readTxtFile(filename);
		for (Map.Entry<String, String> entry : filemaps.entrySet()) {
			System.out.println(entry.getKey()+":"+entry.getValue());
		}
		
	}
	
	
	public static Map<String,String> readTxtFile(String filePath) {
		//存放内容的map对象
		Map<String,String> filemaps = new HashMap<String,String>();
		try {
			String encoding = "UTF-8";
			File file = new File(filePath);
			//System.out.println(filePath);
			int count = 0;//定义顺序变量
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {//按行读取
					if(!"".equals(lineTxt)){
						String reds = lineTxt.split("\\+")[0];//对行的内容进行分析处理后再放入map里。
						int i = reds.indexOf("=");
						reds = reds.substring(1, i)+reds.substring(i+1);
						//System.out.println(reds);
						  
						filemaps.put("第"+count+"站", reds);//放入map
						count ++;
					}
				}
				read.close();//关闭InputStreamReader 
				bufferedReader.close();//关闭BufferedReader 
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
		return filemaps;
	}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值