void openFile(String fileName) {
try {
// Open a file of the given name.
File file = new File(fileName); // Get the size of the opened file.
int size = (int) file.length(); // Set to zero a counter for counting the number of
// characters that have been read from the file.
int chars_read = 0; // Create an input reader based on the file, so we can read its data.
// We handle encoding here.
// We can also use FileReader to handle international character encoding
// conversions.
// FileReader in = new FileReader(file);
FileInputStream infile = new FileInputStream(file);
InputStreamReader in = new InputStreamReader(infile, curCharset);
//curCharset can be "GBK" , "UTF-8" . // Create a character array of the size of the file,
// to use as a data buffer, into which we will read
// the text data.
char[] data = new char[size]; // Read all available characters into the buffer.
while (in.ready()) {
// Increment the count for each character read,
// and accumulate them in the data buffer.
chars_read += in.read(data, chars_read, size - chars_read);
} in.close(); // Create a temporary string containing the data,
// and set the string into the JTextArea.
jTextArea1.setText(new String(data, 0, chars_read));
// Cache the currently opened filename for use at save time...
this.currFileName = fileName;
// ...and the edit session as being clean
this.dirty = false;
jButton2.setEnabled(dirty);
updateCaption(); // Display the name of the opened directory+file in the statusBar.
statusBar.setText("Opened " + fileName);
} catch (IOException e) {
statusBar.setText("Error opening " + fileName);
}
}
try {
// Open a file of the given name.
File file = new File(fileName); // Get the size of the opened file.
int size = (int) file.length(); // Set to zero a counter for counting the number of
// characters that have been read from the file.
int chars_read = 0; // Create an input reader based on the file, so we can read its data.
// We handle encoding here.
// We can also use FileReader to handle international character encoding
// conversions.
// FileReader in = new FileReader(file);
FileInputStream infile = new FileInputStream(file);
InputStreamReader in = new InputStreamReader(infile, curCharset);
//curCharset can be "GBK" , "UTF-8" . // Create a character array of the size of the file,
// to use as a data buffer, into which we will read
// the text data.
char[] data = new char[size]; // Read all available characters into the buffer.
while (in.ready()) {
// Increment the count for each character read,
// and accumulate them in the data buffer.
chars_read += in.read(data, chars_read, size - chars_read);
} in.close(); // Create a temporary string containing the data,
// and set the string into the JTextArea.
jTextArea1.setText(new String(data, 0, chars_read));
// Cache the currently opened filename for use at save time...
this.currFileName = fileName;
// ...and the edit session as being clean
this.dirty = false;
jButton2.setEnabled(dirty);
updateCaption(); // Display the name of the opened directory+file in the statusBar.
statusBar.setText("Opened " + fileName);
} catch (IOException e) {
statusBar.setText("Error opening " + fileName);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货