gpt4 book ai didi

java - 读取文件的有效方法

转载 作者:行者123 更新时间:2023-12-01 06:34:03 24 4
gpt4 key购买 nike

如何在java中高效地读取文件?建议任何方法..我使用BufferedReader,它非常慢..建议任何更改??

代码:

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.StringBuffer;

/*The main class file starts here*/

class TitleCaseRow10 extends JFrame implements ActionListener,FocusListener
{

// Declare the required variables

JButton jbFileLoad,jbFileSave,jbCaseChange;
JLabel jlTitle,jlFileSelect,jlContentDisplay;
Font myFont;
JPanel mainPanel,fileOperationPanel,buttonPanel,fileOpenPanel;
JFrame jframe;
JTextField jtfFileName;
JTextArea jtaFileContent,jtaCaseChange;
JScrollPane jScrPaneContent,jScrPaneCaseChange;
boolean boolFileOk = false;
File file;
String fileName,strFileContent,strBuffer="";
JFileChooser fileChooser;

// Constructor method for TitleCaseRow10

TitleCaseRow10(String titleName)
{
super(titleName);

}

void createAndShowGUI()

/* This method is used to create the Graphical User Interface
and show it in the screen.
This method takes no input parameters
nor does it return any value.
*/

{

// create and set up the window

jframe = new JFrame();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(500,600);

// create the panels
// 1. mainPanel for containing other panels
// 2. buttonPanel for containing buttons
// 3. fileOpenPanel for containing file choose text filed
// and load file button
// 4. fileOperationPanel for containing all the file operation objects

mainPanel = new JPanel();
buttonPanel = new JPanel();
fileOperationPanel = new JPanel();
fileOpenPanel = new JPanel();

// create a file chooser
fileChooser = new JFileChooser();

// create labels and buttons

jlTitle = new JLabel("TITLE CASE CHANGER",JLabel.CENTER);
jlFileSelect = new JLabel("Enter the filename or click 'load file'");
jlContentDisplay = new JLabel("TEXT DISPLAY AREAS",JLabel.CENTER);
jbFileLoad = new JButton("Load File");
jbFileSave = new JButton("Save File");
jbCaseChange = new JButton("Title Case");

//Create text feild, text area and scroll panes

jtfFileName = new JTextField(28);
jtaFileContent = new JTextArea(30,10);
jtaCaseChange = new JTextArea(30,10);
jScrPaneContent = new JScrollPane(jtaFileContent);
jScrPaneCaseChange = new JScrollPane(jtaCaseChange);


// set up the panels

fileOperationPanel.setBackground(Color.BLACK);
fileOpenPanel.setBackground(Color.BLACK);
buttonPanel.setBackground(Color.BLACK);
mainPanel.setBackground(Color.BLACK);
mainPanel.setLayout(new GridLayout(3,0,15,15));
fileOperationPanel.setLayout(new GridLayout(5,0,5,5));
fileOpenPanel.setLayout(new FlowLayout());
buttonPanel.setLayout(new FlowLayout());

// set up text field, labels and text area

// set the font
myFont = new Font("Constantia",1,15);
jtfFileName.setFont(myFont);
jlTitle.setFont(myFont);
jlContentDisplay.setFont(myFont);
jtaFileContent.setFont(myFont);
jtaCaseChange.setFont(myFont);
jlFileSelect.setFont(new Font("Arial",0,14));

// set line wrap
jtaFileContent.setLineWrap(true);
jtaCaseChange.setLineWrap(true);

//set foreground and background color
jlTitle.setForeground(Color.WHITE);
jlContentDisplay.setForeground(Color.WHITE);
jlFileSelect.setForeground(Color.WHITE);
jtaFileContent.setForeground(Color.LIGHT_GRAY);
jtaCaseChange.setForeground(Color.LIGHT_GRAY);
jtfFileName.setForeground(Color.LIGHT_GRAY);
jtaFileContent.setBackground(Color.DARK_GRAY);
jtaCaseChange.setBackground(Color.DARK_GRAY);
jtfFileName.setBackground(Color.DARK_GRAY);

// Set default text to file content text area
jtaFileContent.setText("Type text here");

// set up scroll panes

jScrPaneContent.setAutoscrolls(true);
jScrPaneCaseChange.setAutoscrolls(true);
jScrPaneCaseChange.getHorizontalScrollBar();
jScrPaneCaseChange.getHorizontalScrollBar();

// add the action listeners to buttons

jbFileLoad.addActionListener(this);
jbFileSave.addActionListener(this);
jbCaseChange.addActionListener(this);
jtaFileContent.addFocusListener(this);

// add all the components

jframe.add(mainPanel);
fileOpenPanel.add(jtfFileName);
fileOpenPanel.add(jbFileLoad);
buttonPanel.add(jbCaseChange);
buttonPanel.add(jbFileSave);
fileOperationPanel.add(jlTitle);
fileOperationPanel.add(jlFileSelect);
fileOperationPanel.add(fileOpenPanel);
fileOperationPanel.add(buttonPanel);
fileOperationPanel.add(jlContentDisplay);
mainPanel.add(fileOperationPanel);
mainPanel.add(jScrPaneContent);
mainPanel.add(jScrPaneCaseChange);

// set the frame visible

jframe.setVisible(true);

}

public void actionPerformed(ActionEvent ae)
{
String command = ae.getActionCommand();
if(command.equals("Load File"))
{
System.out.println("Load File action");
fileName = "";
int retVal = fileChooser.showOpenDialog(this);
System.out.println("open dialog...");
if(retVal==JFileChooser.APPROVE_OPTION)
{
try
{
file = fileChooser.getSelectedFile();
fileName = fileChooser.getCurrentDirectory().getPath();
fileName = fileName.concat("\\"+file.getName());
jtfFileName.setText(fileName);
boolFileOk=true;
jtaFileContent.setText("");
BufferedReader br= new BufferedReader(new FileReader(file),1000000);
while((strFileContent = br.readLine())!=null)
jtaFileContent.append(strFileContent+"\n");
}
catch(Exception e)
{
System.out.println("Some problem : "+e);
}
}

}
if(command.equals("Title Case"))
System.out.println("Title Case action");
if(command.equals("Save File"))
System.out.println("Save File action");

}

public void focusGained(FocusEvent fe)
{
System.out.println("Focus Gained");
if(!boolFileOk)
jtaFileContent.setText("");
}
public void focusLost(FocusEvent fe)
{

System.out.println("Focus Lost");
}

/*

boolean loadFile(ActionEvent ae)
{

// Check if the filename textfield is really empty

if(jtfFileName.getText().trim().equals(""))

{
fileName = "";
int retVal = fileChooser.showOpenDialog(TitleCaseRow10.this);
System.out.println("open dialog...");
if(retVal==JFileChooser.APPROVE_OPTION)
{
try
{
file = fileChooser.getSelectedFile();
fileName = fileChooser.getCurrentDirectory().getPath();
fileName = fileName.concat("\\"+file.getName());
jtfFileName.setText(fileName);
return true;
}
catch(Exception e)
{
System.out.println("Some problem : "+e);
}
}
}

return false;
// returns false if the filename is empty or
// if the file is not opened
}

*/


//*****************************************************************

//the main method which creates an
//instance of the class R10TitleCase

//******************************************************************

public static void main(String args[])
{
TitleCaseRow10 caseChanger = new TitleCaseRow10("Title Case");
caseChanger.createAndShowGUI();
}
}

最佳答案

定义“高效”。这取决于您的访问模式。

当您一次从文件中读取一个字节(或几个字节)时,BufferedReader 会加快速度,否则这是无用的开销。尽管如此,它不应该“非常慢”,除非您在代码中做错了什么或者有不切实际的期望。

如果您正在读取非常大的文件,特别是 java.nio FileChannel比java.io有更好的性能。

编辑:您似乎一次一行地将文件的内容添加到 JTextArea (我认为这就是 jtaFileContent ) - 这就是慢的地方(因为它每次都要更新UI,这涉及很多工作),而不是读取文件。相反,您应该将所有文本读取到 StringBuilder 中,并且只有在获得所有内容后才更新 JTextArea。

但是,如果您的文件非常大,您可能会耗尽内存,并且可能必须寻找不同的 UI 组件来仅加载正在显示的部分;那里应该有类似的东西。

关于java - 读取文件的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1081865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com