當前位置: 首頁>>代碼示例>>Java>>正文


Java Formatter.format方法代碼示例

本文整理匯總了Java中java.util.Formatter.format方法的典型用法代碼示例。如果您正苦於以下問題:Java Formatter.format方法的具體用法?Java Formatter.format怎麽用?Java Formatter.format使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.Formatter的用法示例。


在下文中一共展示了Formatter.format方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: doGet

import java.util.Formatter; //導入方法依賴的package包/類
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{

    // create a new formatter
    StringBuffer buffer = new StringBuffer();
    Formatter formatter = new Formatter(buffer, Locale.US);
    String format = "The customer: %s %s" + request.getParameter("suffix");

    //test cases
    formatter.format(format, "John", "Smith", "Jr"); //BAD
    formatter.format(Locale.US, format, "John", "Smith"); //BAD
    //false positive test
    formatter.format("The customer: %s %s", "John", request.getParameter("testParam")); //OK
    
    System.out.printf(format, "John", "Smith"); //BAD
    System.out.printf(Locale.US, format, "John", "Smith"); //BAD

    System.out.format(format, "John", "Smith"); //BAD
    System.out.format(Locale.US, format, "John", "Smith"); //BAD

    String.format(format, "John", "Smith"); //BAD
    String.format(Locale.US, format, "John", "Smith"); //BAD

    }
 
開發者ID:blackarbiter,項目名稱:Android_Code_Arbiter,代碼行數:24,代碼來源:FormatStringManipulation.java

示例2: toString

import java.util.Formatter; //導入方法依賴的package包/類
@Override
public String toString() {
    // Output into coordinate format. Indices start from 1 instead of 0
    Formatter out = new Formatter();

    out.format("%10d %10d %19d%n", numRows, numColumns,
            Matrices.cardinality(this));

    int i = 0;
    for (MatrixEntry e : this) {
        if (e.get() != 0)
            out.format("%10d %10d % .12e\n", e.row() + 1, e.column() + 1,
                    e.get());
        if (++i == 100) {
            out.format("...\n");
            break;
        }
    }

    return out.toString();
}
 
開發者ID:opprop-benchmarks,項目名稱:matrix-toolkits-java,代碼行數:22,代碼來源:AbstractMatrix.java

示例3: buildSensorControlCommand

import java.util.Formatter; //導入方法依賴的package包/類
/**
 * build TDV string of the sensor control command
 * @param info     sensor info
 * @param value    command value
 * @return TDV command string
 */
public static String buildSensorControlCommand(SensorInfo info, int value) {
    Formatter formatter = new Formatter();
    formatter.format("%02x", info.getType().getValueTDVTypes()[0]);
    switch (info.getType()) {
        case BUZZER:
            formatter.format("%02x", ValueType.UCHAR.getCode());
            formatter.format("%02x", value);
            break;
        case LED:
            formatter.format("%02x", ValueType.CHAR3.getCode());
            formatter.format("%06x", value);
            break;
        default:
            formatter.format("%02x", ValueType.BOOL.getCode());
            formatter.format("%02x", value);
            break;
    }
    String result = formatter.toString();
    formatter.close();
    return result;
}
 
開發者ID:SKT-ThingPlug,項目名稱:thingplug-sdk-android,代碼行數:28,代碼來源:TDVBuilder.java

示例4: formatInjectionPoint

import java.util.Formatter; //導入方法依賴的package包/類
public static void formatInjectionPoint(Formatter formatter, Dependency<?> dependency,
    InjectionPoint injectionPoint, ElementSource elementSource) {
  Member member = injectionPoint.getMember();
  Class<? extends Member> memberType = Classes.memberType(member);

  if (memberType == Field.class) {
    dependency = injectionPoint.getDependencies().get(0);
    formatter.format("  while locating %s%n", convert(dependency.getKey(), elementSource));
    formatter.format("    for field at %s%n", StackTraceElements.forMember(member));

  } else if (dependency != null) {
    formatter.format("  while locating %s%n", convert(dependency.getKey(), elementSource));
    formatter.format("    for %s%n", formatParameter(dependency));

  } else {
    formatSource(formatter, injectionPoint.getMember());
  }
}
 
開發者ID:maetrive,項目名稱:businessworks,代碼行數:19,代碼來源:Errors.java

示例5: dumpSerialStream

import java.util.Formatter; //導入方法依賴的package包/類
/**
 * Utility method to dump a byte array in a java syntax.
 * @param bytes and array of bytes
 * @return a string containing the bytes formatted in java syntax
 */
protected static String dumpSerialStream(byte[] bytes) {
    StringBuilder sb = new StringBuilder(bytes.length * 5);
    Formatter fmt = new Formatter(sb);
    fmt.format("    byte[] bytes = {" );
    final int linelen = 10;
    for (int i = 0; i < bytes.length; i++) {
        if (i % linelen == 0) {
            fmt.format("%n        ");
        }
        fmt.format(" %3d,", bytes[i] & 0xff);
        if ((i % linelen) == (linelen-1) || i == bytes.length - 1) {
            fmt.format("  /*");
            int s = i / linelen * linelen;
            int k = i % linelen;
            for (int j = 0; j <= k && s + j < bytes.length; j++) {
                fmt.format(" %c", bytes[s + j] & 0xff);
            }
            fmt.format(" */");
        }
    }
    fmt.format("%n    };%n");
    return sb.toString();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:29,代碼來源:AbstractTCKTest.java

示例6: getRequiredCapability

import java.util.Formatter; //導入方法依賴的package包/類
/**
 * Gets a capability provided by the {@link GraalRuntime} instance available to the application.
 *
 * @throws UnsupportedOperationException if the capability is not available
 */
public static <T> T getRequiredCapability(Class<T> clazz) {
    T t = getRuntime().getCapability(clazz);
    if (t == null) {
        String javaHome = System.getProperty("java.home");
        String vmName = System.getProperty("java.vm.name");
        Formatter errorMessage = new Formatter();
        if (getRuntime().getClass() == InvalidGraalRuntime.class) {
            errorMessage.format("The VM does not support the Graal API.%n");
        } else {
            errorMessage.format("The VM does not expose required Graal capability %s.%n", clazz.getName());
        }
        errorMessage.format("Currently used Java home directory is %s.%n", javaHome);
        errorMessage.format("Currently used VM configuration is: %s", vmName);
        throw new UnsupportedOperationException(errorMessage.toString());
    }
    return t;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:23,代碼來源:Graal.java

示例7: bytesToString

import java.util.Formatter; //導入方法依賴的package包/類
public static String bytesToString(long bytes) {
	Formatter formatter = new Formatter();
	try {
		if (bytes > 1024) {
			if (bytes > 1024L * 1024L) {
				if (bytes > 1024L * 1024L * 1024L) {
					if (bytes > 1024L * 1024L * 1024L * 1024L) {
						return formatter.format("%9.2f", ((float) bytes / (float) (1024L * 1024L * 1024L * 1024L))) + " TB";
					} else {
						return formatter.format("%9.2f", (float) bytes / (float) (1024L * 1024L * 1024L)) + " GB";
					}
				} else {
					return formatter.format("%9.2f", ((float) bytes / (float) (1024L * 1024L))) + " MB";
				}
			} else {
				return formatter.format("%9.2f", ((float) bytes / (float) 1024L)) + " KB";
			}
		} else {
			return "" + bytes + " B";
		}
	} finally {
		formatter.close();
	}
}
 
開發者ID:shenan4321,項目名稱:BIMplatform,代碼行數:25,代碼來源:Formatters.java

示例8: formatSource

import java.util.Formatter; //導入方法依賴的package包/類
public static void formatSource(Formatter formatter, Object source, ElementSource elementSource) {
  String modules = moduleSourceString(elementSource);
  if (source instanceof Dependency) {
    Dependency<?> dependency = (Dependency<?>) source;
    InjectionPoint injectionPoint = dependency.getInjectionPoint();
    if (injectionPoint != null) {
      formatInjectionPoint(formatter, dependency, injectionPoint, elementSource);
    } else {
      formatSource(formatter, dependency.getKey(), elementSource);
    }

  } else if (source instanceof InjectionPoint) {
    formatInjectionPoint(formatter, null, (InjectionPoint) source, elementSource);

  } else if (source instanceof Class) {
    formatter.format("  at %s%s%n", StackTraceElements.forType((Class<?>) source), modules);

  } else if (source instanceof Member) {
    formatter.format("  at %s%s%n", StackTraceElements.forMember((Member) source), modules);

  } else if (source instanceof TypeLiteral) {
    formatter.format("  while locating %s%s%n", source, modules);

  } else if (source instanceof Key) {
    Key<?> key = (Key<?>) source;
    formatter.format("  while locating %s%n", convert(key, elementSource));

  } else if (source instanceof Thread) {
    formatter.format("  in thread %s%n", source);

  } else {
    formatter.format("  at %s%s%n", source, modules);
  }
}
 
開發者ID:maetrive,項目名稱:businessworks,代碼行數:35,代碼來源:Errors.java

示例9: byteToHex

import java.util.Formatter; //導入方法依賴的package包/類
private static String byteToHex(final byte[] hash) {
    Formatter formatter = new Formatter();
    for (byte b : hash)
    {
        formatter.format("%02x", b);
    }
    String result = formatter.toString();
    formatter.close();
    return result;
}
 
開發者ID:Yunfeng,項目名稱:weixinpay,代碼行數:11,代碼來源:SignUtil.java

示例10: formatTo

import java.util.Formatter; //導入方法依賴的package包/類
@Override
public void formatTo(Formatter formatter, int flags, int width, int precision) {
    if ((flags & ALTERNATE) == 0) {
        formatter.format(applyFormattingFlagsAndWidth(toString(), flags, width));
    } else {
        StringBuilder sb = new StringBuilder();
        sb.append(info.method.getName()).append('(');
        String sep = "";
        for (int i = 0; i < info.getParameterCount(); i++) {
            if (info.isConstantParameter(i)) {
                sb.append(sep);
                if (info.getParameterName(i) != null) {
                    sb.append(info.getParameterName(i));
                } else {
                    sb.append(i);
                }
                sb.append('=').append(values[i]);
                sep = ", ";
            }
        }
        sb.append(")");
        String string = sb.toString();
        if (string.indexOf('%') != -1) {
            // Quote any % signs
            string = string.replace("%", "%%");
        }
        formatter.format(applyFormattingFlagsAndWidth(string, flags & ~ALTERNATE, width));
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:30,代碼來源:SnippetTemplate.java

示例11: toString

import java.util.Formatter; //導入方法依賴的package包/類
@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb, Locale.US);
    f.format("=== Types ===%n");
    f.format("%s%n", types.toString());
    f.format("=== Methods ===%n");
    f.format("%s%n", methods.toString());
    f.format("=== Fields ===%n");
    f.format("%s%n", fields.toString());
    return sb.toString();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:DeprDB.java

示例12: bytesToHexString

import java.util.Formatter; //導入方法依賴的package包/類
public static String bytesToHexString(byte[] bytes) {
    StringBuilder sb = new StringBuilder(bytes.length * 2);
    Formatter formatter = new Formatter(sb);
    for (byte b : bytes) {
        formatter.format("%02x", b);
    }
    formatter.close();
    return sb.toString();
}
 
開發者ID:AlexeyZatsepin,項目名稱:CP-Tester,代碼行數:10,代碼來源:ContentProviderHelper.java

示例13: BytetohexString

import java.util.Formatter; //導入方法依賴的package包/類
public static String BytetohexString(byte[] b, int len) {
    StringBuilder sb = new StringBuilder(b.length * (2 + 1));
    Formatter formatter = new Formatter(sb);

    for (int i = 0; i < len; i++) {
        if (i < len - 1)
            formatter.format("%02X:", b[i]);
        else
            formatter.format("%02X", b[i]);

    }
    formatter.close();

    return sb.toString();
}
 
開發者ID:UDOOboard,項目名稱:UDOOBluLib-android,代碼行數:16,代碼來源:Conversion.java

示例14: doStepInternal

import java.util.Formatter; //導入方法依賴的package包/類
protected void doStepInternal() {
    try {
       
        Formatter formatter = new Formatter("energy-"+fileTail);
        Vector pos = adatom.getPosition();
        // Move atom along Y-axis, steps by 0.1
        for(int i=0; i<292; i++){ //292
            
            // Return atom to original Z position
            adatom.getPosition().setX(2, -1.6);
            
            // Move atom along Z-axis, steps by 0.1
            for(int j=0; j<213; j++){  //213
                // --PRINT-- 
                formatter.format("%f %7.2f %7.2f %7.2f \n",new Object[] {energy.getDataAsScalar(),pos.getX(0), pos.getX(1), pos.getX(2)});
                
                // Step atom by 0.1 along Z-axis
                adatom.getPosition().setX(2, adatom.getPosition().getX(2) +0.02);
            }
            // Step atom by 0.1 along Y-axis
            adatom.getPosition().setX(1, adatom.getPosition().getX(1) + 0.02);
 
        }
        formatter.close();
    }
    catch (IOException e) {
        
    }
}
 
開發者ID:etomica,項目名稱:etomica,代碼行數:30,代碼來源:IntegratorEnergyMap.java

示例15: toHexString

import java.util.Formatter; //導入方法依賴的package包/類
private static String toHexString(byte[] bytes) {
    Formatter formatter = new Formatter();

    for (byte b : bytes) {
        formatter.format("%02x", b);
    }

    return formatter.toString();
}
 
開發者ID:ukevgen,項目名稱:BizareChat,代碼行數:10,代碼來源:HmacSha1Signature.java


注:本文中的java.util.Formatter.format方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。