zoukankan      html  css  js  c++  java
  • java操作本地text文件

    //按字节读取
    
    public static void readByBytes(String url) {
      File file = new File(url);
      InputStream in = null;
      try {
        in = new FileInputStream(file);
        int temp;
        while ((temp = in.read()) != -1) {
          System.out.println(temp);
        }
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }
    

      

    //按字符读取
    
    public static void readByChar(String url){
      File file = new File(url);
      Reader read = null;
      StringBuffer buff = new StringBuffer();
      char[] arr = new char[1024];
      int cha=0 ;
      try {
        read =new InputStreamReader(new FileInputStream(file),"utf-8");
        while((cha=read.read(arr))!=-1){
          buff.append(new String(arr,0,cha));
        }
        System.out.println(buff.toString());
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }
    

      

    //按行读取
    
    public static void readByLine(String url){
      File file = new File(url);
      try {
        InputStreamReader in = new InputStreamReader(new FileInputStream(file),"UTF-8");
        BufferedReader buff = new BufferedReader(in);
        String text =null;
        while((text = buff.readLine())!=null){
          System.out.println(text);
        }
        } catch (FileNotFoundException e) {
      } catch (UnsupportedEncodingException e) {
      } catch (IOException e) {
      }
    }
    

      

    //按行读取
    
    public static void readByLineScan(String url){
      try {
        Scanner in = new Scanner(new File(url));
        while(in.hasNextLine()){
          String str = in.nextLine();
          System.out.println(str);
        }
        } catch (FileNotFoundException e) {
        }
    }
    

      

    //通过swing控件读取文件
    
    private static void chooseFile() {
      JFileChooser jfc = new JFileChooser();// 初始化文件选择器
      FileNameExtensionFilter filter = new FileNameExtensionFilter("TXT","txt");// 设置文件过滤,TXT为提示用户选择文件类型,txt为文件过滤后缀
      jfc.setFileFilter(filter);// 将文件过滤加载到选择器中
      int returnVal = jfc.showOpenDialog(null);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
      // 获得打开的文件
      File file = jfc.getSelectedFile();
      File out = new File(file.getParent() + "\\jiagkun.txt");
      try {
        InputStreamReader in = new InputStreamReader(
        new FileInputStream(file), "UTF-8");
        BufferedReader buff = new BufferedReader(in);
        BufferedWriter write = new BufferedWriter(new FileWriter(out));
        String text = null;
        while ((text = buff.readLine()) != null) {
          write.write(text + "\n");
        }
        buff.close();
        write.close();
      } catch (FileNotFoundException e1) {
      } catch (UnsupportedEncodingException e1) {
      } catch (IOException e1) {
      }
      }
    }

    //字符串写入文件


    public static void log(String conent) {
    		BufferedWriter out = null;
    		try {
    			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\test2.txt", true)));
    			out.write(conent + "\r\n");
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				out.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
     

      

  • 相关阅读:
    mplayerww-34106 gcc-4.5.1
    再更新ww的mingw MinGW-full-20101119
    mplayer-ww-37356 compile with mingw gcc 4.5.1 修复无法播放wmv
    CodeBlocks_20160621_rev10868_gcc5.3.0
    更新ww的mingw MinGW-full-20101119
    HTML5学习笔记(六)web worker
    HTML5学习笔记(五)存储
    HTML5学习笔记(四)语义元素
    HTML5学习笔记(三)新属性、功能
    HTML5学习笔记(二)新元素和功能
  • 原文地址:https://www.cnblogs.com/lansetuerqi/p/5383794.html
Copyright © 2011-2022 走看看