Opens a text file as a string. Displays a file open dialog if path is null or blank. Returns null if the user cancels the file open dialog. If there is an error, returns a message in the form "Error: message".
(String path)
| 2333 | the file open dialog. If there is an error, returns a |
| 2334 | message in the form "Error: message". */ |
| 2335 | public static String openAsString(String path) { |
| 2336 | if (path==null || path.equals("")) { |
| 2337 | OpenDialog od = new OpenDialog("Open Text File", ""); |
| 2338 | String directory = od.getDirectory(); |
| 2339 | String name = od.getFileName(); |
| 2340 | if (name==null) return null; |
| 2341 | path = directory + name; |
| 2342 | } |
| 2343 | String str = ""; |
| 2344 | File file = new File(path); |
| 2345 | if (!file.exists()) |
| 2346 | return "Error: file not found"; |
| 2347 | try { |
| 2348 | StringBuffer sb = new StringBuffer(5000); |
| 2349 | BufferedReader r = new BufferedReader(new FileReader(file)); |
| 2350 | while (true) { |
| 2351 | String s=r.readLine(); |
| 2352 | if (s==null) |
| 2353 | break; |
| 2354 | else |
| 2355 | sb.append(s+"\n"); |
| 2356 | } |
| 2357 | r.close(); |
| 2358 | str = new String(sb); |
| 2359 | } |
| 2360 | catch (Exception e) { |
| 2361 | str = "Error: "+e.getMessage(); |
| 2362 | } |
| 2363 | return str; |
| 2364 | } |
| 2365 | |
| 2366 | public static ByteBuffer openAsByteBuffer(String path) { |
| 2367 | if (path==null || path.equals("")) { |
no test coverage detected