Java 需要使用JFile选择器从.txt文件读取数据的帮助吗
因此,我尝试使用JFile选择器从.txt文件中读取字符串并将其保存到数组中。我认为我的设置是正确的,但问题是没有任何东西被放入数组中,因为当我显示数组时,它都是空的。我是java新手,所以我不太熟悉使用JFile chooser。窗口弹出,所有内容都可以让我选择一个文件,所以我认为它在某种程度上起作用了。另外,如果你能向我解释关于JFile chooser的所有代码意味着什么,我将非常感激,我拥有的大部分内容都来自视频、在线和书籍,我有点理解,但我肯定需要一些进一步的解释Java 需要使用JFile选择器从.txt文件读取数据的帮助吗,java,arrays,string,jfilechooser,Java,Arrays,String,Jfilechooser,因此,我尝试使用JFile选择器从.txt文件中读取字符串并将其保存到数组中。我认为我的设置是正确的,但问题是没有任何东西被放入数组中,因为当我显示数组时,它都是空的。我是java新手,所以我不太熟悉使用JFile chooser。窗口弹出,所有内容都可以让我选择一个文件,所以我认为它在某种程度上起作用了。另外,如果你能向我解释关于JFile chooser的所有代码意味着什么,我将非常感激,我拥有的大部分内容都来自视频、在线和书籍,我有点理解,但我肯定需要一些进一步的解释 import jav
import java.io.*;
import java.util.Scanner; // All My imports
import javax.swing.*;
public class SpellChecker // I am creating a spell checker
{
static String check[] = new String[50]; // My array to save all of the strings in a file
static int index = 0; // index will incremented whenever a string is added to the array
public static void main(String args[])
{
getFile(); // Method call that opens file and saves all the data to the array
for(int i = 0; i < check.length - 1; i++) // outputting the array
{
System.out.println(check[i]);
}
}
public static void getFile()
{
JFileChooser chooser;
File infile, directory;
int status;
String wordToCheck;
chooser = new JFileChooser( );
status = chooser.showOpenDialog(null);
if(status == JFileChooser.APPROVE_OPTION)
{
infile = chooser.getSelectedFile();
directory = chooser.getCurrentDirectory();
try
{
infile = new File(chooser.getSelectedFile().getAbsolutePath());
Scanner scanner = new Scanner(infile);
while(scanner.hasNext())
{
wordToCheck = scanner.next();
if(index == check.length)
{
String tempAr[] = new String[check.length*2];
for(int i = 0; i < index; i++)
{
tempAr[i] = check[i];
}
check = tempAr;
}
check[index] = wordToCheck;
index++;
}
scanner.close();
}
catch(IOException e)
{
System.out.println("Error");
}
}
else
{
System.out.println("Open File dialog canceled");
}
}
import java.io.*;
导入java.util.Scanner;//我所有的进口商品
导入javax.swing.*;
公共类拼写检查器//我正在创建拼写检查器
{
静态字符串检查[]=新字符串[50];//我的数组将所有字符串保存在一个文件中
static int index=0;//每当向数组中添加字符串时,索引将递增
公共静态void main(字符串参数[])
{
getFile();//打开文件并将所有数据保存到数组的方法调用
for(int i=0;i
是时候找一个调试器并找出问题所在了。另外,为什么要设置两次infle
呢?一次应该足够了。另外,请注意,您的问题似乎与使用JFileChooser没有任何关系。老实说,Idk。我正在偏离我在书籍和在线上看到的东西。我不擅长JFile choose,我在gene中的文件I/O方面也很弱但是我正在努力学习并在这方面做得更好。我使用Eclipse运行了一个调试器,它工作正常,所以我不确定这意味着什么