简体   繁体   English

读写文件java

[英]reading and writing files java

I have a float array which I stored in it some values from user input.我有一个浮点数组,我在其中存储了一些来自用户输入的值。 I have 2 methods one that saves the values stored in the array to a text file each value on a line and the second method rereads the values again and stores them in the array.我有 2 种方法,一种将存储在数组中的值保存到文本文件中,每个值在一行中,第二种方法再次重新读取值并将它们存储在数组中。 for example, the user input was 1,2,3,4 I save them to a text file and then I read the same txt file now my array should display 8 elements 1,2,3,4,1,2,3,4.例如,用户输入是 1,2,3,4 我将它们保存到一个文本文件然后我读取相同的 txt 文件现在我的数组应该显示 8 个元素 1,2,3,4,1,2,3, 4. the problem I'm having is that when I store these elements on the txt file it's storing them and adding like 100 zeros under them and when I'm calling the second method to reread these elements from the file it reads the zeros so when I'm displaying the elements in my array it's displaying 0,0,0,0 when it should display 1,2,3,4,1,2,3,4.我遇到的问题是,当我将这些元素存储在 txt 文件中时,它会存储它们并在它们下面添加 100 个零,当我调用第二种方法从文件中重新读取这些元素时,它会读取零,所以当我'正在显示我的数组中的元素,当它应该显示 1,2,3,4,1,2,3,4 时它显示 0,0,0,0。 what might be causing me this problem?什么可能导致我这个问题?

public void saveValuesToFile(Scanner keyboard) {
    try {
        System.out.println("Enter name of file: ");
        String fileName = keyboard.next();
        File file = new File(fileName);
        PrintWriter outputFile = new PrintWriter(file);

        for(int i = 0; i < numbers.length; i++) {
            outputFile.println(numbers[i]);
        }

        outputFile.close();



    } catch (FileNotFoundException e) {
        System.out.println("file dont exist");
        e.printStackTrace();
    }

}


public void readFromFile(Scanner keyboard) {
    
    System.out.println("Enter file name");
    String fileName = keyboard.next();
    BufferedReader reader = null;
    try {
        reader  = new BufferedReader (new FileReader(fileName));
        String input = null;

        while ((input = reader.readLine()) != null) {
            for (int i = 0; i < numbers.length; i++) {
                numbers[i] = Float.parseFloat(input);
    }
    }
}
    catch (NumberFormatException | IOException e) {
        e.printStackTrace();
 }
}

You may check why the array is populated properly using additional println statement.您可以使用附加的 println 语句检查为什么正确填充了数组。 In your version each element of array is populated with the same element read from the file.在您的版本中,数组的每个元素都填充有从文件中读取的相同元素。 If you remove the inner loop, array will be populated properly.如果删除内部循环,数组将被正确填充。

   int i=0;
    while ((input = reader.readLine()) != null) {
        
         numbers[i] = Float.parseFloat(input);
         System.out.println((i) + "::"+numbers[i]);
         i++;
     }

Zeros are being added because you're saving numbers as float .添加零是因为您将数字保存为float If you store an integer 3 in a float variable it will be converted to a float equivalent which is 3.0如果您将 integer 3存储在浮点变量中,它将被转换为等效的浮点数,即3.0

Also you don't need two loops here,此外,这里不需要两个循环,

while ((input = reader.readLine()) != null) {
            for (int i = 0; i < numbers.length; i++) {
                numbers[i] = Float.parseFloat(input);
    }

You can instead do following,您可以改为执行以下操作,

int i = 0;
while ((input = reader.readLine()) != null) {
   numbers[i] = Float.parseFloat(input);
   i++;
  }

Following is a fully functional program of what you desire,以下是您想要的功能齐全的程序,

import java.util.Scanner;
import java.io.*;

public class Hello {
    
    public static float[] numbers = {1,2,3,4,1,2,3,4};
    
    public static void saveValuesToFile(Scanner keyboard) {
        try {
            System.out.println("Enter name of file: ");
            String fileName = keyboard.next();
            File file = new File(fileName);
            PrintWriter outputFile = new PrintWriter(file);

            for(int i = 0; i < numbers.length; i++) {
                outputFile.println(numbers[i]);
            }

            outputFile.close();



        } catch (FileNotFoundException e) {
            System.out.println("file doesn't exist");
            e.printStackTrace();
        }

    }


    public static void readFromFile(Scanner keyboard) {
        
        System.out.println("Enter file name");
        String fileName = keyboard.next();
        BufferedReader reader = null;
        try {
            reader  = new BufferedReader (new FileReader(fileName));
            String input = null;
            int i = 0;
            while ((input = reader.readLine()) != null) {
                    numbers[i] = Float.parseFloat(input);
                    i++;
            }
            
            for(int j = 0; j < numbers.length; j++) {
                System.out.println(numbers[j]);
            }
        }
        catch (NumberFormatException | IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       saveValuesToFile(scanner);
       readFromFile(scanner);
    }

}
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM