zoukankan      html  css  js  c++  java
  • 数组写入文件和文件读取数组

     数组写入文件和文件读取数组

    //写入文件
    		File file=new File("data.txt");
    		FileWriter out=new FileWriter(file);
    		out.write((num+1)+"
    ");
    		for(int i=0;i<sparseArray.length;i++) {
    			for(int j=0;j<sparseArray[i].length;j++) {
    				out.write(sparseArray[i][j]+"	");
    			}
    			out.write("
    ");
    		}
    		out.close();
    	    
    		System.out.println("--------------------------");
    		
    		//从文件中读取稀疏数组
    		BufferedReader in=new BufferedReader(new FileReader(file));
    		int lines=Integer.parseInt(in.readLine());
    		int arr2[][]=new int[lines][3];
    		String line;
    		String[] temp=null;
    		int row=0;
    		while((line=in.readLine())!=null) {
    			temp=line.split("	");
    			for(int j=0;j<temp.length;j++) {
    				arr2[row][j]=Integer.parseInt(temp[j]);
    			}
    			row++;
    		}
    
    		
    		in.close();
    		System.out.println("从文件中读取的数组:");
    		for(int[] r:arr2) {
    			for(int data:r) {
    				System.out.print(data+"	");
    			}
    			System.out.println();
    		}
    		System.out.println("-------------------------");
  • 相关阅读:
    mysql误删表,无备份
    感情启示录
    奸的好人之财色战场
    Word神器使用
    Maven工程的Web调试
    IntelIoT技术笔记Java/Eclipse
    IntelIoT技术笔记Maven
    Linux脚本(二)
    MINA
    360是神器
  • 原文地址:https://www.cnblogs.com/yh-simon/p/12236667.html
Copyright © 2011-2022 走看看