Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of Java File path

Shulou Source: shulou.com Published: 2022-06-02 03:04:35 09月23日 Update

This article introduces you to the Java file path example analysis, the content is very detailed, interested friends can refer to, hope to be helpful to you.

1. In Servlet web environments such as ServletFilter, as long as you get the javax.servlet.ServletContext type, you can use getRealPath ("...") Get the path. The topmost directory in the relative path can be obtained through the parameter "/".

Request.getSession (). GetServletContext () getRealPath ("/")

2. JSP Custom tag javax.servlet.jsp.tagext.TagSupport

((javax.servlet.ServletContext) pageContext) .getRealPath ("")

3. Used in normal Java Class object files:

ServletContext servletContext = ServletActionContext.getServletContext ()

String pathName = servletContext.getRealPath ("/")

This.getClass () .getResource

If the Class file is in the top-level directory (package) and "??" Is a blank string ("), and execution of this method in the jar file returns null. Each layer directory (package) below the top-level directory (package) returns the URL that contains the protocol. The folders (packages) of each layer are separated by "/".

Project location: C:project directory.

File location: C:projectTest.java

Contents of the file:

/ * source begin. * / public class Test {public Test () {System.out.println (this.getClass (). GetResource (")); System.out.println (this.getClass (). GetResource (". ")); System.out.println (this.getClass (). GetResource (" / ")); System.out.println (this.getClass (). GetResource (" Test.class ");} 4) ServletActionContext.getRequest () .getContextPath ()

Gets the path name configured in Tomcat.

2) A simple example of reading a JAVA file / / reading data into a character list char [] data = new char [10240]; int num=file.read (data); / / converting the character list into a string String str=new String (data,0,num); / / outputting in the console System.out.println ("Characters read=" + num); System.out.println (str); file.close ()

3) ergodic Mapre of Hash MAP. Iteratorit = ret.keySet (). Iterator (); while (it.hasNext ()) {String key = it.next (); String value = ret.get (key). ToString (); System.out.println ("Key:" + key); System.out.println ("Value:" + value);}

4) how to get the current time import java.text.SimpleDateFormat;import java.util.Date;Date today = new Date (); SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd"); return df.format (today)

5) IP address translation in character or host form to integer public static int inet_addr (String src) throws UnknownHostException {InetAddress address = InetAddress.getByName (src); int numeric_ip= 0BYTE [] buf= address.getAddress (); if ((buf! = null) & & (buf.length = = 4)) {numeric_ip= buf [3] & 0xFFX numeric address | = (buf [2]

Tags: File path character directory content top level instance instance analysis analysis location string more help good normal host between examples interests parameters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology Redmi Shulou Information NVidia