简体   繁体   English

Java传递字符串数组

[英]Java passing a string array

I'm pretty new to Java and I'm doing some things for practice. 我刚接触Java,正在做一些练习。 I'm just messing with simple IO stuff right now, reading from a file and then printing to an output. 我现在只是在搞弄简单的IO东西,从文件中读取然后打印到输出。 I also played with removing commas from the input and got that part working. 我还玩过删除输入中的逗号,并使该部分正常工作。 My problem is when I try to print the values in my array into a new file. 我的问题是当我尝试将数组中的值打印到新文件中时。 Is the fact that my array is created inside the while loop for the BufferedReader? 我的数组是在BufferedReader的while循环内创建的事实吗? How can I get the values in the array values to be seen by the BufferedWriter? 如何获得数组值中的values以供BufferedWriter查看? I'm also curious how I would go about reversing the order of the array when printing to the output file? 我也很好奇在打印到输出文件时如何反转数组的顺序? Thanks for any help guys, I've searched but haven't been able to come across anything this specific. 感谢您的帮助,我已经搜索过,但没有遇到任何具体问题。

Code: 码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.io.FileWriter;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("myImport.txt"));
        String line = null;

        while ((line = br.readLine()) != null) {
            String[] values = line.split(",");
            System.out.println("Your file has been evaluated, here are the contents:");
            for (String str : values) {
                System.out.println(str);
            }
        }
        br.close();

        try {

            File file = new File("myOutput.txt");

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(values);
            for (int i = 0; i < values.length; i++) {
                fw.write(values[i] + "\n");
            }
            bw.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The easiest way to make your array visible to the writer is to declare it before setting it. 使数组对作者可见的最简单方法是在设置数组之前对其进行声明。

String line = null;
String[] values = null;

Then inside your while loop, you instantiate it. 然后在while循环中,实例化它。

values = line.split(",");

That way it is visible to the writer. 这样作者就可以看到它。

To access your values array you have to declar it as a class member or at the top of the function: 要访问您的values array您必须将其声明为类成员或在函数顶部:

public class Main {
   String[] values;
}

You would either need to add values in a list and iterate through the list, or you could also write as you read from the file. 您可能需要在列表中添加values并遍历列表,或者还可以在从文件中读取内容时进行写操作。

As the scope of your values is in the while loop reading it, you may choose to write as you print it. 由于值的范围是在while循环中读取的,因此您可以选择在打印时进行写入。 That is: 那是:

while ((line = br.readLine()) != null) {
    String[] values = line.split(",");
    System.out.println("Your file has been evaluated, here are the contents:");
    for (String str : values) {
        System.out.println(str);
        fw.write(str + "\n");
    }
}

You could simply store an ArrayList and add each split line of the file to that list. 您可以简单地存储一个ArrayList并将文件的每个分割行添加到该列表。 Then you can iterate over this list later. 然后,您可以稍后遍历此列表。

List<String[]> linesList = new ArrayList<>();
while ((line = br.readLine()) != null) {
    String[] values = line.split(",");
    linesList.add(values);
    System.out.println("Your file has been evaluated, here are the contents:");
    for (String str : values) {
        System.out.println(str);
    }
}

Then you can iterate over them later... 然后,您可以稍后对其进行迭代...

for (String[] values : linesList) {
    for (var i = values.length - 1; i >= 0; i--) {
        String value = values[i];
        // Store your value here using your FileWriter, etc.
    }
}

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

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