Changeset 7315 in josm


Ignore:
Timestamp:
2014-07-18T23:30:23+02:00 (10 years ago)
Author:
Don-vip
Message:

code simplification with some Java 7 new NIO2 Files methods (newBufferedReader, newBufferedWriter, readAllLines)

Location:
trunk/src/org/openstreetmap/josm
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/OpenFileAction.java

    r7082 r7315  
    1010import java.io.BufferedReader;
    1111import java.io.File;
    12 import java.io.FileInputStream;
    1312import java.io.IOException;
    14 import java.io.InputStreamReader;
    1513import java.nio.charset.StandardCharsets;
     14import java.nio.file.Files;
    1615import java.util.ArrayList;
    1716import java.util.Arrays;
     
    292291
    293292                for (File urlFile: urlFiles) {
    294                     try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(urlFile), StandardCharsets.UTF_8))) {
     293                    try (BufferedReader reader = Files.newBufferedReader(urlFile.toPath(), StandardCharsets.UTF_8)) {
    295294                        String line;
    296295                        while ((line = reader.readLine()) != null) {
  • trunk/src/org/openstreetmap/josm/data/AutosaveTask.java

    r7082 r7315  
    77import java.io.File;
    88import java.io.FileFilter;
    9 import java.io.FileInputStream;
    109import java.io.IOException;
    11 import java.io.InputStreamReader;
    1210import java.io.PrintStream;
    1311import java.lang.management.ManagementFactory;
    1412import java.nio.charset.StandardCharsets;
     13import java.nio.file.Files;
    1514import java.util.ArrayList;
    1615import java.util.Date;
     
    303302                File pidFile = getPidFile(file);
    304303                if (pidFile.exists()) {
    305                     try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(pidFile), StandardCharsets.UTF_8))) {
     304                    try (BufferedReader reader = Files.newBufferedReader(pidFile.toPath(), StandardCharsets.UTF_8)) {
    306305                        String jvmId = reader.readLine();
    307306                        if (jvmId != null) {
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r7248 r7315  
    88import java.io.BufferedReader;
    99import java.io.File;
    10 import java.io.FileInputStream;
    1110import java.io.FileOutputStream;
    1211import java.io.IOException;
    1312import java.io.InputStream;
    14 import java.io.InputStreamReader;
    1513import java.io.OutputStreamWriter;
    1614import java.io.PrintWriter;
     
    2018import java.lang.reflect.Field;
    2119import java.nio.charset.StandardCharsets;
     20import java.nio.file.Files;
    2221import java.util.ArrayList;
    2322import java.util.Collection;
     
    5756import org.openstreetmap.josm.tools.I18n;
    5857import org.openstreetmap.josm.tools.Utils;
     58import org.xml.sax.SAXException;
    5959
    6060/**
     
    790790    }
    791791
    792     public void load() throws Exception {
     792    /**
     793     * Loads preferences from settings file.
     794     * @throws IOException if any I/O error occurs while reading the file
     795     * @throws SAXException if the settings file does not contain valid XML
     796     * @throws XMLStreamException if an XML error occurs while parsing the file (after validation)
     797     */
     798    public void load() throws IOException, SAXException, XMLStreamException {
    793799        settingsMap.clear();
    794800        File pref = getPreferenceFile();
    795         try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(pref), StandardCharsets.UTF_8))) {
     801        try (BufferedReader in = Files.newBufferedReader(pref.toPath(), StandardCharsets.UTF_8)) {
    796802            validateXML(in);
    797803        }
    798         try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(pref), StandardCharsets.UTF_8))) {
     804        try (BufferedReader in = Files.newBufferedReader(pref.toPath(), StandardCharsets.UTF_8)) {
    799805            fromXML(in);
    800806        }
     
    803809    }
    804810
    805     public void init(boolean reset){
     811    /**
     812     * Initializes preferences.
     813     * @param reset if {@code true}, current settings file is replaced by the default one
     814     */
     815    public void init(boolean reset) {
    806816        // get the preferences.
    807817        File prefDir = getPreferencesDirFile();
     
    13881398    protected XMLStreamReader parser;
    13891399
    1390     public void validateXML(Reader in) throws Exception {
     1400    public void validateXML(Reader in) throws IOException, SAXException {
    13911401        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    13921402        try (InputStream xsdStream = new CachedFile("resource://data/preferences.xsd").getInputStream()) {
  • trunk/src/org/openstreetmap/josm/data/validation/OsmValidator.java

    r7082 r7315  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.BufferedReader;
    76import java.io.File;
    8 import java.io.FileInputStream;
    97import java.io.FileNotFoundException;
    108import java.io.FileOutputStream;
    119import java.io.IOException;
    12 import java.io.InputStreamReader;
    1310import java.io.OutputStreamWriter;
    1411import java.io.PrintWriter;
    1512import java.nio.charset.StandardCharsets;
     13import java.nio.file.Files;
     14import java.nio.file.Path;
     15import java.nio.file.Paths;
    1616import java.util.ArrayList;
    1717import java.util.Arrays;
     
    170170        ignoredErrors.clear();
    171171        if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
    172             File file = new File(getValidatorDir() + "ignorederrors");
    173             if (file.exists()) {
    174                 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
    175                     for (String line = in.readLine(); line != null; line = in.readLine()) {
    176                         ignoredErrors.add(line);
    177                     }
     172            Path path = Paths.get(getValidatorDir() + "ignorederrors");
     173            if (Files.exists(path)) {
     174                try {
     175                    ignoredErrors.addAll(Files.readAllLines(path, StandardCharsets.UTF_8));
    178176                } catch (final FileNotFoundException e) {
    179177                    Main.debug(Main.getErrorMessage(e));
  • trunk/src/org/openstreetmap/josm/io/GeoJSONExporter.java

    r7214 r7315  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.BufferedWriter;
    76import java.io.File;
    8 import java.io.FileOutputStream;
    97import java.io.IOException;
    10 import java.io.OutputStreamWriter;
    118import java.io.Writer;
    129import java.nio.charset.StandardCharsets;
     10import java.nio.file.Files;
    1311
    1412import org.openstreetmap.josm.actions.ExtensionFileFilter;
     
    3230        if (layer instanceof OsmDataLayer) {
    3331            String json = new GeoJSONWriter((OsmDataLayer) layer).write();
    34             try (Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) {
     32            try (Writer out = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
    3533                out.write(json);
    3634            }
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookUnixoid.java

    r7314 r7315  
    1010import java.io.BufferedReader;
    1111import java.io.File;
    12 import java.io.FileInputStream;
    1312import java.io.IOException;
    1413import java.io.InputStreamReader;
     
    1716import java.nio.charset.StandardCharsets;
    1817import java.nio.file.Files;
     18import java.nio.file.Path;
    1919import java.nio.file.Paths;
    2020import java.security.KeyStore;
     
    278278            String result = null;
    279279            if (path != null) {
    280                 File file = new File(path);
    281                 if (file.exists()) {
    282                     try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
     280                Path p = Paths.get(path);
     281                if (Files.exists(p)) {
     282                    try (BufferedReader reader = Files.newBufferedReader(p, StandardCharsets.UTF_8)) {
    283283                        String id = null;
    284284                        String release = null;
Note: See TracChangeset for help on using the changeset viewer.