高效的文件阅读方式

时间:2022-08-24 12:16:46

how to read a file in an efficient way in java?? suggest any methods.. i used BufferedReader,which is very slow.. suggest any changes??

如何在java中以有效的方式读取文件?建议任何方法..我使用BufferedReader,这是非常慢..建议任何变化?

CODE:

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();
}
}

4 个解决方案

#1


Define "efficient". It depends on your access pattern.

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

BufferedReader is going to speed things up when you read from the file one byte (or a few bytes) at a time, otherwise it's useless overhead. Still, it shouldn't be "very slow" unless you're doing something wrong in your code or have unrealistic expectations.

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

If you're reading very large files, java.nio, speficically FileChannel has much better performance than java.io.

如果您正在阅读非常大的文件,java.nio,特别是FileChannel的性能要比java.io好得多。

Edit: You seem to be adding the file's contents to a JTextArea (I assume that's what jtaFileContent is) one line at a time - that's what's slow (because it has to update the UI every time, which involves a LOT of work), not the reading of the file. Instead, you should read all the text into a StringBuilder and only update the JTextArea once you have everything.

编辑:您似乎将文件的内容添加到JTextArea(我假设这是jtaFileContent是什么)一次一行 - 这是什么慢(因为它必须每次更新UI,这涉及很多工作),而不是阅读文件。相反,您应该将所有文本读入StringBuilder,并且只有在拥有所有内容后才更新JTextArea。

However, if your file is very large, you'll probably run out of memory and may have to look for a different UI component that loads only the part that is being shown; there should be something like that out there.

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

#2


Are you sure it's actually the file IO which is slow? If you just append the same number of lines using a hard-coded string, is it fast? I wouldn't be surprised if it were the GUI which was slowing you down.

你确定它的文件IO实际上很慢吗?如果您只是使用硬编码字符串附加相同数量的行,它会快吗?如果是降低你速度的GUI,我不会感到惊讶。

By the way, FileReader will use the platform default encoding, which is usually a bad idea. Use a FileInputStream and an InputStreamReader with a specific encoding. (Then wrap it in a BufferedReader of course.) You should also have a try/finally to close the reader at the end.

顺便说一句,FileReader将使用平台默认编码,这通常是一个坏主意。使用具有特定编码的FileInputStream和InputStreamReader。 (然后当然将它包装在BufferedReader中。)你也应该尝试/最后在最后关闭阅读器。

#3


The problem is that you're appending directly into jtaFileContent. Try reading into a StringBuilder and then calling jtaFileContent.setText()

问题是你直接附加到jtaFileContent。尝试读入StringBuilder,然后调用jtaFileContent.setText()

StringBuilder sb = new StringBuilder(100);
for(Scanner sc = new Scanner(new FileReader(fileName)); sc.hasNext(); )
  sb.append(sc.nextLine());

jtaFileContent.setText(sb.toString());

#4


Here's a few helpful methods. Between these three methods you can read a file or a resource into a String. I use them all the time. I hope you find them useful:

这是一些有用的方法。在这三种方法之间,您可以将文件或资源读入String。我一直都在使用它们。我希望你发现它们很有用:

/**
* Takes the file and returns it in a string
*
* @param location on the disc
* @return the file in string form
* @throws IOException
*/
public static String fileToString(String location) throws IOException {
  FileReader fr = new FileReader(new File(location));
  return readerToString(fr);
}

/**
* Takes the given resource (based on the given class) and returns that as a string.
*
* @param location relative to the given class (for example "/resources/textFile.txt")
* @param c the class the location is relative to
* @return the file in string form
* @throws IOException
*/
public static String resourceToString(String location, Class c) throws IOException {
  InputStream is = c.getResourceAsStream(location);
  InputStreamReader r = new InputStreamReader(is);
  return readerToString(r);
}

/**
* Returns all the lines in the scanner's stream as a String
*
* @param r the InputStreamReader to read
* @return the InputStreamReader in String form
* @throws IOException
*/
public static String readerToString(InputStreamReader r) throws IOException {
  StringWriter sw = new StringWriter();
  char[] buf = new char[1024];
  int len;
  while ((len = r.read(buf)) > 0) {
    sw.write(buf, 0, len);
  }
  r.close();
  sw.close();
  return sw.toString();
}

#1


Define "efficient". It depends on your access pattern.

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

BufferedReader is going to speed things up when you read from the file one byte (or a few bytes) at a time, otherwise it's useless overhead. Still, it shouldn't be "very slow" unless you're doing something wrong in your code or have unrealistic expectations.

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

If you're reading very large files, java.nio, speficically FileChannel has much better performance than java.io.

如果您正在阅读非常大的文件,java.nio,特别是FileChannel的性能要比java.io好得多。

Edit: You seem to be adding the file's contents to a JTextArea (I assume that's what jtaFileContent is) one line at a time - that's what's slow (because it has to update the UI every time, which involves a LOT of work), not the reading of the file. Instead, you should read all the text into a StringBuilder and only update the JTextArea once you have everything.

编辑:您似乎将文件的内容添加到JTextArea(我假设这是jtaFileContent是什么)一次一行 - 这是什么慢(因为它必须每次更新UI,这涉及很多工作),而不是阅读文件。相反,您应该将所有文本读入StringBuilder,并且只有在拥有所有内容后才更新JTextArea。

However, if your file is very large, you'll probably run out of memory and may have to look for a different UI component that loads only the part that is being shown; there should be something like that out there.

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

#2


Are you sure it's actually the file IO which is slow? If you just append the same number of lines using a hard-coded string, is it fast? I wouldn't be surprised if it were the GUI which was slowing you down.

你确定它的文件IO实际上很慢吗?如果您只是使用硬编码字符串附加相同数量的行,它会快吗?如果是降低你速度的GUI,我不会感到惊讶。

By the way, FileReader will use the platform default encoding, which is usually a bad idea. Use a FileInputStream and an InputStreamReader with a specific encoding. (Then wrap it in a BufferedReader of course.) You should also have a try/finally to close the reader at the end.

顺便说一句,FileReader将使用平台默认编码,这通常是一个坏主意。使用具有特定编码的FileInputStream和InputStreamReader。 (然后当然将它包装在BufferedReader中。)你也应该尝试/最后在最后关闭阅读器。

#3


The problem is that you're appending directly into jtaFileContent. Try reading into a StringBuilder and then calling jtaFileContent.setText()

问题是你直接附加到jtaFileContent。尝试读入StringBuilder,然后调用jtaFileContent.setText()

StringBuilder sb = new StringBuilder(100);
for(Scanner sc = new Scanner(new FileReader(fileName)); sc.hasNext(); )
  sb.append(sc.nextLine());

jtaFileContent.setText(sb.toString());

#4


Here's a few helpful methods. Between these three methods you can read a file or a resource into a String. I use them all the time. I hope you find them useful:

这是一些有用的方法。在这三种方法之间,您可以将文件或资源读入String。我一直都在使用它们。我希望你发现它们很有用:

/**
* Takes the file and returns it in a string
*
* @param location on the disc
* @return the file in string form
* @throws IOException
*/
public static String fileToString(String location) throws IOException {
  FileReader fr = new FileReader(new File(location));
  return readerToString(fr);
}

/**
* Takes the given resource (based on the given class) and returns that as a string.
*
* @param location relative to the given class (for example "/resources/textFile.txt")
* @param c the class the location is relative to
* @return the file in string form
* @throws IOException
*/
public static String resourceToString(String location, Class c) throws IOException {
  InputStream is = c.getResourceAsStream(location);
  InputStreamReader r = new InputStreamReader(is);
  return readerToString(r);
}

/**
* Returns all the lines in the scanner's stream as a String
*
* @param r the InputStreamReader to read
* @return the InputStreamReader in String form
* @throws IOException
*/
public static String readerToString(InputStreamReader r) throws IOException {
  StringWriter sw = new StringWriter();
  char[] buf = new char[1024];
  int len;
  while ((len = r.read(buf)) > 0) {
    sw.write(buf, 0, len);
  }
  r.close();
  sw.close();
  return sw.toString();
}