young man you know nothing about io

I recently ran into a problem with io. A 4G file needs me to read it out, and then process it and put it into the database. 4G, it just felt a little bigger when I got it. What are you afraid of, read it out, deal with it, ok. save. . . . . .

Well, let's go.

@RequestMapping("xxxx")
   @ResponseBody
   @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
   public  String readText(HttpServletRequest request,String type){File file = new File("xxxx.json");
StringBuilder result = new StringBuilder();
      try{
            
         BufferedReader br = new BufferedReader( new FileReader(file)) ; // Construct a BufferedReader class to read the file
          String s = null;
          while ((s = br.readLine())!= null ){ // Use the readLine method, Read one line of JSONObject at a time json = JSONObject.parseObject (s) ;
             //Process business
            
         }
         br.close();
      }catch(Exception e){
         e.printStackTrace();
      }
      return result.toString();
   }
Obviously, fail. Can't read it at all. It's too big, so hurry up and find foreign aid. Then the boss said to use the stream to process (well, the stream), and you have to find a way to cut the data. . . . . Huge embarrassment. Well, crawling crawling, still borrowed the code of the boss. What is the face? For this question, if you don't know it, you should learn from it, and you should summarize it in a timely manner.

@RequestMapping("/xxxx")
@ResponseBody
public String uploadAreaPersonLocal(HttpServletRequest request, String fileName, String type) throws JSONErrorException {
   Map<String,Object> map = new HashMap<>();
   Reader reader = null;
   int count = 0;
   try {
      StringBuilder builder = new StringBuilder() ;
       int charread = 0 ;
       JSONArray array = new JSONArray() ;
       reader = new InputStreamReader( new FileInputStream(fileName)) ;
 // Read multiple characters into character array, charread is one read Number of characters
 while ((charread = reader.read()) != - 1 ) {
          if (( char )charread == '[' ) {
             break;
 }                     
      }
      while ((charread = reader.read()) != -1) {
         char tempChar = (char)charread;
         if (tempChar == ']') {
            break;
         }
         if (tempChar == '{') {
            builder.setLength(0);
         }
         builder.append(tempChar);
         if (tempChar == '}') {
            array.add(JSON.parseObject(builder.toString()));
            builder.setLength(0);
         }
         if (array.size() >= 10000) {
            array = new JSONArray();
            count = count + 10000;}
         
      }
      if (array.size() > 0) {
         if (Integer.valueOf(type) == 1) {
            this.cityDataService.saveLivePeople(array);
         } else if(Integer.valueOf(type) == 2) {
            this.cityDataService.saveDailyPeople(array);
         }
         System.out.println("已保存:" + (count + array.size()));
      }
   } catch (Exception e1) {
      e1.printStackTrace();
   } finally {
      if (reader != null) {
         try {
            reader.close();
         } catch (IOException e1) {
         }
      }
   }
   map.put(Constant.CODE , "0" ) ;
 map.put (Constant.MESSAGE , "success" ) ;
 logger .info( " City custom area population data saved " ) ;
    return JSON.toJSONString ( map )
 ; }      

BufferedReader

Knowledge points: BufferReader, Reader, File, FileInputStream

主要对IO的理解不够深刻。那么,需要认真的看一看io了。



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325486854&siteId=291194637