zoukankan      html  css  js  c++  java
  • java基础知识回顾——将数据存到txt文件中同时从TXT文件中提取数据

    今天在做数据结构的作业时,用到了文件的数据存储与读取,当时由于距离上次写这方面的代码时间有点长了,有点懵,所以决定这次老老实实的写篇博客记录一下:

     1 package 数据结构第二阶段幼儿评测;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.File;
     5 import java.io.FileReader;
     6 import java.io.FileWriter;
     7 
     8 
     9 public class Secondlevel {
    10 
    11     private static double[] dArrs;
    12 
    13     public static void main(String[] args) {
    14         
    15         System.out.println("文件测试数据如下");
    16         try {
    17             double[] file = getFile("数据结构第二阶段.txt");
    18             
    19             for(int i=0;i<file.length;i++) {
    20                 
    21                     System.out.print(file[i]+" ");
    22                 
    23             }
    24         } catch (Exception e) {
    25             // TODO Auto-generated catch block
    26             e.printStackTrace();
    27         }
    28 
    29         System.out.println();
    30         System.out.println("正在写入文件数据......");
    31         try {
    32             toFile("inputFile.txt");
    33         } catch (Exception e) {
    34             // TODO Auto-generated catch block
    35             e.printStackTrace();
    36         }
    37         
    38     }
    39 
    40     //从文件中读取数据
    41     private static double[] getFile(String pathName) throws Exception {
    42         //【1】先创建一个File的实体对象
    43         File file = new File(pathName);
    44         if (!file.exists())
    45             throw new RuntimeException("找不到文件!");
    46         //【2】加载BUfferedReader流
    47         BufferedReader br = new BufferedReader(new FileReader(file));
    48         String str;
    49         
    50         //【3】一行一行读取
    51         while ((str = br.readLine()) != null) {
    52             
    53             int s = 0;
    54             //文件中数据的分割我用的是‘,’具体根据自己的情况调用下面的split()函数
    55             String[] arr = str.split(",");
    56             
    57             dArrs = new double[arr.length];
    58             for(int i=0;i<arr.length;i++) {
    59                 String string=arr[i];
    60                 int parseInt = Integer.parseInt(string);
    61                 dArrs[i]=parseInt;
    62                 
    63             }        
    64         }    
    65         return dArrs ;
    66     }
    67 
    68     //将数据存储到文件中
    69     private static void toFile(String path) throws Exception{
    70         File file=null;
    71         FileWriter fWriter=null;
    72         file=new File(path);
    73         try {
    74             if(!file.exists()) {
    75                 System.out.println("要读入数据的文件不存在");
    76             }
    77             fWriter=new FileWriter(file);
    78             fWriter.write(String.valueOf(dArrs[0]));
    79             fWriter.flush();
    80             System.out.println("写入数据成功!");
    81         }catch (Exception e) {
    82             // TODO: handle exception
    83             e.printStackTrace();
    84         }finally {
    85             if (fWriter!=null) {
    86                 fWriter.close();
    87             }
    88         }
    89         
    90         
    91         
    92     }
    93 }
  • 相关阅读:
    dotnet 新项目格式与对应框架预定义的宏
    dotnet 线程静态字段
    dotnet 线程静态字段
    dotnet 通过 WMI 拿到显卡信息
    dotnet 通过 WMI 拿到显卡信息
    dotnet 通过 WMI 获取指定进程的输入命令行
    dotnet 通过 WMI 获取指定进程的输入命令行
    dotnet 通过 WMI 获取系统信息
    dotnet 通过 WMI 获取系统信息
    PHP show_source() 函数
  • 原文地址:https://www.cnblogs.com/zhang188660586/p/11120202.html
Copyright © 2011-2022 走看看