【发布时间】:2014-12-28 08:44:15
【问题描述】:
我应该创建一个 java 小程序项目,我需要从 .csv 文件中读取数据并将其放入可滚动表中。我试图这样做,但我得到了 FileNotFoundException 异常。
这是我的代码:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
@SuppressWarnings("serial")
public class Test extends Applet
{
Table table;
public void init()
{
try
{
table = new Table();
}
catch (Exception e)
{
e.printStackTrace();
}
this.add(table);
}
}
class Table extends JPanel
{
private String[][] productList;
private JTable table;
public Table() throws Exception
{
read();
BorderLayout layout = new BorderLayout();
layout.addLayoutComponent(new JScrollPane(table), BorderLayout.CENTER);
setLayout(layout);
}
private void read() throws Exception
{
ArrayList<String[]> list = new ArrayList<String[]>();
try
{
BufferedReader br = new BufferedReader(new FileReader("Products.csv"));
String line = br.readLine();
while ((line = br.readLine()) != null)
{
String[] toBeAdded = line.split(",");
list.add(toBeAdded);
}
}
catch (Exception e)
{
e.printStackTrace();
}
productList = new String[list.size()][3];
for (int i = 0; i < productList.length; i++)
for (int j = 0; j < productList[i].length; j++)
productList[i][j] = list.get(i)[j];
}
}
products.csv 位于根文件夹中。我尝试阅读一个正常的非摇摆项目,它可以工作,但不适用于这个。谁能帮我?
【问题讨论】:
标签: java applet java-io filenotfoundexception