Skip to content

Commit c6f1660

Browse files
committed
Simplify files read/write in examples
1 parent 508ad4a commit c6f1660

File tree

3 files changed

+25
-57
lines changed

3 files changed

+25
-57
lines changed

examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/addressbook/AddressBook.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2022 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -17,14 +17,15 @@
1717
import static org.eclipse.swt.events.SelectionListener.widgetDefaultSelectedAdapter;
1818
import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;
1919

20-
import java.io.BufferedReader;
2120
import java.io.File;
2221
import java.io.FileNotFoundException;
23-
import java.io.FileReader;
24-
import java.io.FileWriter;
2522
import java.io.IOException;
23+
import java.nio.file.Files;
24+
import java.nio.file.StandardOpenOption;
25+
import java.util.ArrayList;
2626
import java.util.Arrays;
2727
import java.util.Comparator;
28+
import java.util.List;
2829
import java.util.ResourceBundle;
2930

3031
import org.eclipse.swt.SWT;
@@ -287,18 +288,9 @@ private void openAddressBook() {
287288
Cursor waitCursor = shell.getDisplay().getSystemCursor(SWT.CURSOR_WAIT);
288289
shell.setCursor(waitCursor);
289290

290-
String[] data = new String[0];
291-
try (FileReader fileReader = new FileReader(file.getAbsolutePath());
292-
BufferedReader bufferedReader = new BufferedReader(fileReader);){
293-
294-
String nextLine = bufferedReader.readLine();
295-
while (nextLine != null){
296-
String[] newData = new String[data.length + 1];
297-
System.arraycopy(data, 0, newData, 0, data.length);
298-
newData[data.length] = nextLine;
299-
data = newData;
300-
nextLine = bufferedReader.readLine();
301-
}
291+
List<String> data = new ArrayList<>();
292+
try {
293+
data = Files.readAllLines(file.toPath());
302294
} catch(FileNotFoundException e) {
303295
displayError(resAddressBook.getString("File_not_found") + "\n" + file.getName());
304296
return;
@@ -309,13 +301,13 @@ private void openAddressBook() {
309301
shell.setCursor(null);
310302
}
311303

312-
String[][] tableInfo = new String[data.length][table.getColumnCount()];
304+
String[][] tableInfo = new String[data.size()][table.getColumnCount()];
313305
int writeIndex = 0;
314306
for (String element : data) {
315307
String[] line = decodeLine(element);
316308
if (line != null) tableInfo[writeIndex++] = line;
317309
}
318-
if (writeIndex != data.length) {
310+
if (writeIndex != data.size()) {
319311
String[][] result = new String[writeIndex][table.getColumnCount()];
320312
System.arraycopy(tableInfo, 0, result, 0, writeIndex);
321313
tableInfo = result;
@@ -346,10 +338,8 @@ private boolean save() {
346338
lines[i] = encodeLine(itemText);
347339
}
348340

349-
try (FileWriter fileWriter = new FileWriter(file.getAbsolutePath(), false);){
350-
for (String line : lines) {
351-
fileWriter.write(line);
352-
}
341+
try {
342+
Files.write(file.toPath(), List.of(lines), StandardOpenOption.TRUNCATE_EXISTING);
353343
} catch(FileNotFoundException e) {
354344
displayError(resAddressBook.getString("File_not_found") + "\n" + file.getName());
355345
return false;

examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/javaviewer/JavaViewer.java

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2017 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -15,13 +15,10 @@
1515

1616
import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;
1717

18-
import java.io.BufferedReader;
1918
import java.io.File;
20-
import java.io.FileInputStream;
2119
import java.io.FileNotFoundException;
2220
import java.io.IOException;
23-
import java.io.InputStreamReader;
24-
import java.io.Reader;
21+
import java.nio.file.Files;
2522
import java.text.MessageFormat;
2623
import java.util.ResourceBundle;
2724

@@ -145,28 +142,16 @@ void open(String name) {
145142
}
146143

147144
try {
148-
FileInputStream stream= new FileInputStream(file.getPath());
149-
try (Reader in = new BufferedReader(new InputStreamReader(stream))) {
150-
151-
char[] readBuffer= new char[2048];
152-
StringBuilder buffer= new StringBuilder((int) file.length());
153-
int n;
154-
while ((n = in.read(readBuffer)) > 0) {
155-
buffer.append(readBuffer, 0, n);
156-
}
157-
textString = buffer.toString();
158-
stream.close();
159-
} catch (IOException e) {
160-
// Err_file_io
161-
String message = MessageFormat.format(resources.getString("Err_file_io"), file.getName());
162-
displayError(message);
163-
return;
164-
}
165-
}
166-
catch (FileNotFoundException e) {
145+
textString = Files.readString(file.toPath());
146+
} catch (FileNotFoundException e) {
167147
String message = MessageFormat.format(resources.getString("Err_not_found"), file.getName());
168148
displayError(message);
169149
return;
150+
} catch (IOException e) {
151+
// Err_file_io
152+
String message = MessageFormat.format(resources.getString("Err_file_io"), file.getName());
153+
displayError(message);
154+
return;
170155
}
171156
// Guard against superfluous mouse move events -- defer action until later
172157
Display display = text.getDisplay();

examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet133.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2018 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -25,6 +25,7 @@
2525
import static org.eclipse.swt.events.SelectionListener.*;
2626

2727
import java.io.*;
28+
import java.nio.file.*;
2829

2930
import org.eclipse.swt.*;
3031
import org.eclipse.swt.graphics.*;
@@ -109,16 +110,8 @@ void menuOpen() {
109110
return;
110111

111112
File file = new File(name);
112-
try (FileInputStream stream = new FileInputStream(file.getPath())) {
113-
Reader in = new BufferedReader(new InputStreamReader(stream));
114-
char[] readBuffer = new char[2048];
115-
StringBuilder buffer = new StringBuilder((int) file.length());
116-
int n;
117-
while ((n = in.read(readBuffer)) > 0) {
118-
buffer.append(readBuffer, 0, n);
119-
}
120-
textString = buffer.toString();
121-
stream.close();
113+
try {
114+
textString = Files.readString(file.toPath());
122115
} catch (FileNotFoundException e) {
123116
MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
124117
box.setMessage("File not found:\n" + name);

0 commit comments

Comments
 (0)