• 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();
    			}
    		}
    	}
     

      

  • 相关阅读:
    java基础(七) java四种访问权限
    java基础(六) switch语句的深入解析
    JavaSe: 不要小看了 Serializable
    对于培训出身的同学,接下来该怎么学习技术?
    Java Tomcat7性能监控与优化详解
    模仿spring-aop的功能,利用注解搭建自己的框架。
    动态页面技术EL
    如何在mysql客户端即mysql提示符下执行操作系统命令
    通过notepad++将混乱的xml配置的格式进行美化
    shell脚本中,for基于列表进行循环的实现方法
  • 原文地址:https://www.cnblogs.com/lansetuerqi/p/5383794.html
Copyright © 2020-2023  润新知