Java Code Examples for java.nio.file.InvalidPathException

Following code examples demonstrate how to use java.nio.file.InvalidPathExceptionfrom java. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use java.nio.file.InvalidPathExceptionand various code implementation of this class.

    public static String toPath(String path , String filename) throws InvalidPathException{
        if (StringUtils.isNotBlank(path) ){
            if (path.trim().endsWith("/") || path.trim().endsWith("/")){
                return path+(filename!=null ? filename : "");
            }
            return path+"/"+(filename!=null ? filename : "");

        }
        throw new InvalidPathException("wrong path configuration cannot find path -> ",path);
    } 


  public InvalidPathException demarshall(EJValue a0, MarshallingSession a1) {
    lazyInit();
    EJObject obj = a0.isObject();
    if (obj == null) {
      return null;
    }
    String objId = obj.get("^ObjectID").isString().stringValue();
    if (a1.hasObject(objId)) {
      return a1.getObject(InvalidPathException.class, objId);
    }
    InvalidPathException entity = new InvalidPathException();
    a1.recordObject(objId, entity);
    if ((obj.containsKey("input")) && (!obj.get("input").isNull())) {
      _$1078371949__1195259493_input(entity, java_lang_String.demarshall(obj.get("input"), a1));
    }
    if ((obj.containsKey("reason")) && (!obj.get("reason").isNull())) {
      _$1078371949__1195259493_reason(entity, java_lang_String.demarshall(obj.get("reason"), a1));
    }
    if ((obj.containsKey("index")) && (!obj.get("index").isNull())) {
      _$1078371949__104431_index(entity, java_lang_Integer.demarshall(obj.get("index"), a1));
    }
    if ((obj.containsKey("detailMessage")) && (!obj.get("detailMessage").isNull())) {
      _1630335596__1195259493_detailMessage(entity, java_lang_String.demarshall(obj.get("detailMessage"), a1));
    }
    if ((obj.containsKey("cause")) && (!obj.get("cause").isNull())) {
      entity.initCause(java_lang_Throwable.demarshall(obj.get("cause"), a1));
    }
    if ((obj.containsKey("stackTrace")) && (!obj.get("stackTrace").isNull())) {
      entity.setStackTrace(arrayOf_java_lang_StackTraceElement_D1.demarshall(obj.get("stackTrace"), a1));
    }
    return entity;
  } 

	public static boolean isCorrect(String path) {
		if (path == null && path.equals("")) throw new IllegalArgumentException("path cannot be null or empty");
		try {
			Paths.get(path);
        }catch (InvalidPathException e) {
			return false;
		}
		return true;
	} 

    public Path parse(String text) throws ParseException {
        try {
            return Paths.get(text);
        } catch(InvalidPathException e) {
            throw new FormatException(e.getMessage());
        }
    } 

    public static void main(String[] args) {
        String p = "?";
        try {
            Paths.get(p);
        } catch (InvalidPathException e) {
            System.out.println("EXC");
            System.out.println(e.getMessage());
        }
    } 

	public Optional<Path> read(String value) {
		try {
			return Optional.of(Paths.get(value));
		} catch (InvalidPathException e) {
			return Optional.empty();
		}
	} 

    public static void main(String[] args) {
        int i;

        try (InputStream fin = Files.newInputStream(Paths.get("file1.txt"))) {

            do {
                i = fin.read();
                if (i != -1) System.out.print((char) i);
            } while (i != -1);
        } catch (InvalidPathException e) {
            System.out.print(e.getMessage());
        }catch (IOException e ) {
            System.out.print(e.getMessage());
        }
    } 

	public static void main(String[] args) {
		assert args != null;
		for (String name : args) {
			try {
				ArrayList<Double> list = ReadFile.readList(name);
				for (Double d : list) {
					System.out.printf(" %.2f%n", d);
				}
			} catch (NumberFormatException | InvalidPathException | IOException e) {
				System.out.println(e);
			}
			System.out.println("end of file: " + name + "\n\n" );
		}
	} 

	public static void main(String args[]) {
		if (args.length != 2) {
			System.out.println("Пpимeнeниe: необходимо ввести путь откуда и куда копировать");
			return;
		}
		try {
			Path source = Paths.get(args[0]);
			Path target = Paths.get(args[1]);
			Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
		} catch (InvalidPathException e) {
			System.out.println("Oшибкa указания пути: " + e);
		} catch (IOException e) {
			System.out.println("Oшибкa ввода-вывода: " + e);
		}
	} 

  public InvalidPathException demarshall(EJValue a0, MarshallingSession a1) {
    lazyInit();
    EJObject obj = a0.isObject();
    if (obj == null) {
      return null;
    }
    String objId = obj.get("^ObjectID").isString().stringValue();
    if (a1.hasObject(objId)) {
      return a1.getObject(InvalidPathException.class, objId);
    }
    InvalidPathException entity = new InvalidPathException();
    a1.recordObject(objId, entity);
    if ((obj.containsKey("input")) && (!obj.get("input").isNull())) {
      _$1078371949__1195259493_input(entity, java_lang_String.demarshall(obj.get("input"), a1));
    }
    if ((obj.containsKey("reason")) && (!obj.get("reason").isNull())) {
      _$1078371949__1195259493_reason(entity, java_lang_String.demarshall(obj.get("reason"), a1));
    }
    if ((obj.containsKey("index")) && (!obj.get("index").isNull())) {
      _$1078371949__104431_index(entity, java_lang_Integer.demarshall(obj.get("index"), a1));
    }
    if ((obj.containsKey("detailMessage")) && (!obj.get("detailMessage").isNull())) {
      _1630335596__1195259493_detailMessage(entity, java_lang_String.demarshall(obj.get("detailMessage"), a1));
    }
    if ((obj.containsKey("cause")) && (!obj.get("cause").isNull())) {
      entity.initCause(java_lang_Throwable.demarshall(obj.get("cause"), a1));
    }
    if ((obj.containsKey("stackTrace")) && (!obj.get("stackTrace").isNull())) {
      entity.setStackTrace(arrayOf_java_lang_StackTraceElement_D1.demarshall(obj.get("stackTrace"), a1));
    }
    return entity;
  } 

    public static void main(String[] args) {
        try (OutputStream fout =
               new BufferedOutputStream
                       (Files.newOutputStream
                               (Paths.get("file4.txt")))){

         for (int i=0; i < 26; i++)
             fout.write(('A' + i) + i);
        } catch (InvalidPathException e) {
            System.out.print(e.getMessage());
        }catch (IOException e) {
            System.out.print(e.getMessage());
        }
    } 

    @Test
    public default void verifyGenericType()
    throws Exception {
        final Class<SUT> sut = createNewSUT();
        assertTrue("This j8unit test interface is used with a generic type that is illegaly not assignable to InvalidPathException.class!",
                   InvalidPathException.class.isAssignableFrom(sut));
    } 

    public static Configuration parse(boolean append, List<String> paths) {
        if (paths == null) {
            throw new IllegalArgumentException("Second argument not can be null");
        }

        boolean error = false;
        List<Path> files = new ArrayList<>();
        for (String path : paths) {
            try {
                files.add(Paths.get(path));
            } catch (InvalidPathException e) {
                error = true;
                System.err.println("Incorrect file path " + path);
            }
        }
        return new Configuration(append, files, error);
    } 

Advertisement
Javadoc
Unchecked exception thrown when path string cannot be converted into a Path because the path string

contains invalid characters, or the path string is invalid for other file system specific reasons.

Read More
Advertisement