diff --git a/examples/org.eclipse.swt.examples/META-INF/MANIFEST.MF b/examples/org.eclipse.swt.examples/META-INF/MANIFEST.MF index 6857f743ceb..9b80453c74f 100644 --- a/examples/org.eclipse.swt.examples/META-INF/MANIFEST.MF +++ b/examples/org.eclipse.swt.examples/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %plugin.SWTStandaloneExampleSet.name Bundle-SymbolicName: org.eclipse.swt.examples; singleton:=true -Bundle-Version: 3.108.900.qualifier +Bundle-Version: 3.108.1000.qualifier Bundle-Vendor: %providerName Bundle-Localization: plugin Bundle-RequiredExecutionEnvironment: JavaSE-17 diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/addressbook/AddressBook.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/addressbook/AddressBook.java index cd30291a69d..5e08a83941e 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/addressbook/AddressBook.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/addressbook/AddressBook.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2022 IBM Corporation and others. + * Copyright (c) 2000, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -17,14 +17,15 @@ import static org.eclipse.swt.events.SelectionListener.widgetDefaultSelectedAdapter; import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter; -import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.FileWriter; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardOpenOption; +import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; +import java.util.List; import java.util.ResourceBundle; import org.eclipse.swt.SWT; @@ -287,18 +288,9 @@ private void openAddressBook() { Cursor waitCursor = shell.getDisplay().getSystemCursor(SWT.CURSOR_WAIT); shell.setCursor(waitCursor); - String[] data = new String[0]; - try (FileReader fileReader = new FileReader(file.getAbsolutePath()); - BufferedReader bufferedReader = new BufferedReader(fileReader);){ - - String nextLine = bufferedReader.readLine(); - while (nextLine != null){ - String[] newData = new String[data.length + 1]; - System.arraycopy(data, 0, newData, 0, data.length); - newData[data.length] = nextLine; - data = newData; - nextLine = bufferedReader.readLine(); - } + List data = new ArrayList<>(); + try { + data = Files.readAllLines(file.toPath()); } catch(FileNotFoundException e) { displayError(resAddressBook.getString("File_not_found") + "\n" + file.getName()); return; @@ -309,13 +301,13 @@ private void openAddressBook() { shell.setCursor(null); } - String[][] tableInfo = new String[data.length][table.getColumnCount()]; + String[][] tableInfo = new String[data.size()][table.getColumnCount()]; int writeIndex = 0; for (String element : data) { String[] line = decodeLine(element); if (line != null) tableInfo[writeIndex++] = line; } - if (writeIndex != data.length) { + if (writeIndex != data.size()) { String[][] result = new String[writeIndex][table.getColumnCount()]; System.arraycopy(tableInfo, 0, result, 0, writeIndex); tableInfo = result; @@ -346,10 +338,8 @@ private boolean save() { lines[i] = encodeLine(itemText); } - try (FileWriter fileWriter = new FileWriter(file.getAbsolutePath(), false);){ - for (String line : lines) { - fileWriter.write(line); - } + try { + Files.write(file.toPath(), List.of(lines), StandardOpenOption.TRUNCATE_EXISTING); } catch(FileNotFoundException e) { displayError(resAddressBook.getString("File_not_found") + "\n" + file.getName()); return false; diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/javaviewer/JavaViewer.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/javaviewer/JavaViewer.java index 64ec400e27e..b22bee0d60b 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/javaviewer/JavaViewer.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/javaviewer/JavaViewer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2017 IBM Corporation and others. + * Copyright (c) 2000, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -15,13 +15,10 @@ import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter; -import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; +import java.nio.file.Files; import java.text.MessageFormat; import java.util.ResourceBundle; @@ -145,28 +142,16 @@ void open(String name) { } try { - FileInputStream stream= new FileInputStream(file.getPath()); - try (Reader in = new BufferedReader(new InputStreamReader(stream))) { - - char[] readBuffer= new char[2048]; - StringBuilder buffer= new StringBuilder((int) file.length()); - int n; - while ((n = in.read(readBuffer)) > 0) { - buffer.append(readBuffer, 0, n); - } - textString = buffer.toString(); - stream.close(); - } catch (IOException e) { - // Err_file_io - String message = MessageFormat.format(resources.getString("Err_file_io"), file.getName()); - displayError(message); - return; - } - } - catch (FileNotFoundException e) { + textString = Files.readString(file.toPath()); + } catch (FileNotFoundException e) { String message = MessageFormat.format(resources.getString("Err_not_found"), file.getName()); displayError(message); return; + } catch (IOException e) { + // Err_file_io + String message = MessageFormat.format(resources.getString("Err_file_io"), file.getName()); + displayError(message); + return; } // Guard against superfluous mouse move events -- defer action until later Display display = text.getDisplay(); diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet133.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet133.java index ee7c34162c2..3d1c8b12fd5 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet133.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet133.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2018 IBM Corporation and others. + * Copyright (c) 2000, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -25,6 +25,7 @@ import static org.eclipse.swt.events.SelectionListener.*; import java.io.*; +import java.nio.file.*; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; @@ -109,16 +110,8 @@ void menuOpen() { return; File file = new File(name); - try (FileInputStream stream = new FileInputStream(file.getPath())) { - Reader in = new BufferedReader(new InputStreamReader(stream)); - char[] readBuffer = new char[2048]; - StringBuilder buffer = new StringBuilder((int) file.length()); - int n; - while ((n = in.read(readBuffer)) > 0) { - buffer.append(readBuffer, 0, n); - } - textString = buffer.toString(); - stream.close(); + try { + textString = Files.readString(file.toPath()); } catch (FileNotFoundException e) { MessageBox box = new MessageBox(shell, SWT.ICON_ERROR); box.setMessage("File not found:\n" + name);