java呼叫window本地應用程式;讀取TXT型別檔案

zip it.發表於2020-11-27
//用.bat執行命令開啟window本地應用,bat內為cmd命令
		String strcmd = "cmd /c start /b d:\\ocr\\b.bat";
		Runtime rt=Runtime.getRuntime();
		Process ps=null;
		try{
			ps=rt.exec(strcmd);
			ps.waitFor();
		}catch(IOException e1){
			e1.printStackTrace();
		}catch(InterruptedException e){
			e.printStackTrace();
		}
		int i=ps.exitValue();
		if(i==0){
			System.out.println("執行完成!");
		}else{
			System.out.println("執行失敗!");
		}
		ps.destroy();
		ps=null;
//直接用寫cmd執行命令開啟window本地應用:
		BufferedReader br =null;
		try {
		StringBuilder sb =new StringBuilder();
		Process p=Runtime.getRuntime().exec("cmd /c start /b  && d: && cd d:\\ocr\\ && XXX.exe");// /b為不顯示cmd視窗,關閉命令為taskkill /f /t /im XXX.exe
		br =new BufferedReader(new InputStreamReader(p.getInputStream()));
		String line=null;
		while((line =br.readLine())!=null) {
			sb.append(line+" \n");
		}
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(br!=null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
//讀取TXT檔案:
String filePath="d:/ocr/work/~TMP0001.TXT";
		File file=new File(filePath);
		BufferedReader reader=null;
		String tempString =null;
		int line=1;
		List<String> wenbenlist=new ArrayList<String>();
		try{
			reader=new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
			while((tempString=reader.readLine())!=null){
				wenbenlist.add(tempString);
				line++;
			}
			System.out.println(wenbenlist);
			reader.close();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e1){
			e1.printStackTrace();
		}finally{
			if(reader!=null){
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

相關文章