zoukankan      html  css  js  c++  java
  • 用过的读写

    1、从文件中读取三行数据

     1 String[] lines = new String[3];
     2 boolean readPointerFileSuccess = true;
     3 
     4 try (Scanner scanner = new Scanner(pointerFile)) {
     5     for (int i = 0; i < lines.length; i++) {
     6         if (scanner.hasNextLine()) {
     7             lines[i] = scanner.nextLine();
     8         } else {
     9             readPointerFileSuccess = false;
    10         }
    11     }
    12 }
    View Code

     2、写入文件

    1 try(PrintWriter writer = new PrintWriter(pointerFile)){
    2     writer.println(logFileName);
    3     writer.println(pointer);
    4     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    5     String time = sdf.format(readDate);
    6     writer.println(time);
    7     writer.flush();//立即写入
    8 }
    View Code

    3、RandomAccessFile读取数据

     1 RandomAccessFile randAccessFile = new RandomAccessFile(fileName, "r");
     2 // 定位到文件的 pointer
     3 randAccessFile.seek(pointer);
     4 
     5 String line = readLine(randAccessFile, "GBK");
     6         
     7         
     8 public String readLine(RandomAccessFile randAccessFile, String charset) throws IOException {
     9 
    10 ByteArrayOutputStream baos = new ByteArrayOutputStream();
    11 
    12 int c = -1;
    13 boolean eol = false;
    14 
    15 while (!eol) {
    16     switch (c = randAccessFile.read()) {
    17     case -1:
    18     case '
    ':
    19         eol = true;
    20         break;
    21     case '
    ':
    22         eol = true;
    23         long cur = randAccessFile.getFilePointer();
    24         if ((randAccessFile.read()) != '
    ') {
    25             randAccessFile.seek(cur);
    26         }
    27         break;
    28     default:
    29         baos.write(c);
    30         break;
    31     }
    32 }
    33 
    34 if ((c == -1) && (baos.size() == 0)) {
    35     return null;
    36 }
    37 
    38 return new String(baos.toByteArray(), charset);
    39 }
    View Code

    4、RandomAccessFile读取数据

     1     File file = new File(info.getName());
     2 
     3     RandomAccessFile raf = null;
     4     try {
     5         raf = new RandomAccessFile(file, "rw");
     6     } catch (FileNotFoundException e) {
     7         e.printStackTrace();
     8     }
     9 
    10     FileChannel cha = raf.getChannel();
    11 
    12     ByteBuffer buf = ByteBuffer.allocate(1024);
    13 
    14     int size = 0;
    15 
    16     StringBuffer sb = new StringBuffer();
    17 
    18     try {
    19         while ((size = cha.read(buf)) != -1) {
    20 
    21             buf.flip();
    22 
    23             byte[] buff = new byte[size];
    24 
    25             buf.get(buff);
    26 
    27             sb.append(new String(buff, 0, size));
    28 
    29         }
    30     } catch (IOException e) {
    31         e.printStackTrace();
    32     }
    33 
    34     String[] s = sb.toString().split("\}\,\{");
    View Code
    这个博客主要是javaEE相关或者不相关的记录, hadoop与spark的相关文章我写在下面地址的博客啦~ http://www.cnblogs.com/sorco
  • 相关阅读:
    整除15问题
    软件工程基础Proposal提议
    对在大学阶段软件工程实践的一些想法
    运行web项目端口占用问题
    Day_1
    error C3615: constexpr 函数 "QAlgorithmsPrivate::qt_builtin_ctz" 不会生成常数表达式
    Qt应用程序的打包
    将html代码部署到阿里云服务器,并进行域名解析,以及在部署过程中遇到的问题和解决方法
    linux部署html代码到linux服务器,并进行域名解析
    运行sudo apt-get install nginx时报错有几个软件包无法下载,要不运行 apt-get update 或者加上 --fix-missing 的选项再试试?解决
  • 原文地址:https://www.cnblogs.com/orco/p/6223163.html
Copyright © 2011-2022 走看看