本文整理汇总了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
}
示例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();
}
示例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;
}
示例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());
}
}
示例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();
}
示例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;
}
示例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();
}
}
示例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);
}
}
示例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;
}
示例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));
}
}
示例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();
}
示例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();
}
示例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();
}
示例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) {
}
}
示例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();
}