Java+Android技巧

1、获取所在行

StackTraceElement[] stacks = Thread.currentThread().getStackTrace();   
        String str = "类名:"+stacks[2].getClassName() + "\n函数名:" + stacks[2].getMethodName()  
        + "\n文件名:" + stacks[2].getFileName() + "\n行号:"  
        + stacks[2].getLineNumber() + "";  
        MyLogger.d(TAG, str);
        MyLogger.d(TAG, Thread.currentThread().getStackTrace()[2].getLineNumber()+"行");

2、获取数据库表

	public static String convertDatabaseCharsetType(String in, String type) {
		String dbUser;
		if (in != null) {
			if (type.equals("oracle")) {
				dbUser = in.toUpperCase();
			} else if (type.equals("postgresql")) {
				dbUser = "public";
			} else if (type.equals("mysql")) {
				dbUser = null;
			} else if (type.equals("mssqlserver")) {
				dbUser = null;
			} else if (type.equals("db2")) {
				dbUser = in.toUpperCase();
			} else {
				dbUser = in;
			}
		} else {
			dbUser = "public";
		}
		return dbUser;
	}
	private static void getTables(Connection conn) throws SQLException {
		DatabaseMetaData dbMetData = conn.getMetaData();
		ResultSet rs = dbMetData.getTables(null, convertDatabaseCharsetType(
				"root","mysql"), null, new String[]{ "TABLE", "VIEW" });

		while (rs.next()) {
			if (rs.getString(4) != null
					&& (rs.getString(4).equalsIgnoreCase("TABLE") || rs
							.getString(4).equalsIgnoreCase("VIEW"))) {
				System.out.println(rs.getString(3).toLowerCase());
			}
		}

	}
	private static void getTables2(Connection conn) throws SQLException {
		DatabaseMetaData dbMetData = conn.getMetaData();
		ResultSet rs = dbMetData.getTables(null, "root", null, new String[]{ "TABLE", "VIEW" });
		while (rs.next()) {
			System.out.println(rs.getString(3).toLowerCase());			
		}

	}

3、Java重启:

public static void restartApplication() throws URISyntaxException, IOException
	{
	  final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
	  final File currentJar = new File(Start.class.getProtectionDomain().getCodeSource().getLocation().toURI());
	  if(!currentJar.getName().endsWith(".jar"))
	    return;
	  final ArrayList<String> command = new ArrayList<String>();
	  command.add(javaBin);
	  command.add("-jar");
	  command.add(currentJar.getPath());
	  final ProcessBuilder builder = new ProcessBuilder(command);
	  builder.start();
	  System.exit(0);
	}

4、java管道通讯

public InputStream getInputStream(final FileHeader hd) throws RarException,
			IOException {
		final PipedInputStream in = new PipedInputStream(32 * 1024);
		final PipedOutputStream out = new PipedOutputStream(in);

		// creates a new thread that will write data to the pipe. Data will be
		// available in another InputStream, connected to the OutputStream.
		new Thread(new Runnable() {
			public void run() {
				try {
					extractFile(hd, out);
				} catch (RarException e) {
				} finally {
					try {
						out.close();
					} catch (IOException e) {
					}
				}
			}
		}).start();

		return in;
	}


你可能感兴趣的:(Java+Android技巧)