有没有办法从带有颜色格式数据的进程中捕获控制台输出?目前我只捕获文本输出:
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "mvn dependency:resolve");
// mvn dependency:resolve is an example of a process that outputs color
Process p = builder.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
Run Code Online (Sandbox Code Playgroud)
但是我找不到捕获此文本颜色数据的方法。彩色文本似乎没有以任何特殊字符或任何东西开头。
我正在将此捕获的文本打印到 UI 以供用户查看此过程的日志,但很难阅读,因此我想从控制台复制颜色。
这是一个如何在 Go 中完成的示例,但这可以在 Java 中完成吗?
这个应用程序将在 Windows 上运行,但如果它也可以通过从 Shell 或 Bash 读取相同的颜色数据,也可以在 Linux 或 MacOS 上运行,那就太好了。
您好我需要将Mongo文档转换为DBObject(BasicDBObject).
我正在使用GridFS将文件上传到mongo,我想设置元数据,这是我在文档中获得的.我知道Document与DBObject几乎相同.我知道我可以这样做:
Document doc = new Document();
BasicDBObject.parse(doc.toJson());
Run Code Online (Sandbox Code Playgroud)
但是,这不必要的表现是不是很重?
gridFS方法setMetaData()只接受DBObject所以我必须转换它.
是否有更好的方法来做到这一点而不是将其转换为字符串并返回?
即使该应用被终止或设备进入睡眠状态,我也需要在每个时间段运行一段代码。
我正在使用它AlarmManager来实现这一目标,但是它可以正常工作,但是当设备休眠约五分钟时,Service它不再被调用了……有人知道我在做什么错吗?
这是我的代码:
public class Profiler extends IntentService {
public static final int ALARM_MANAGER_ID = 21436587;
public Profiler() {
super("Profiler");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
JodaTimeAndroid.init(getApplicationContext());
System.out.println("Testing profiles");
List<Profile> profiles = MainActivity.getStoredProfiles(getApplicationContext());
if (profiles != null) {
for (Profile profile : profiles) {
if(profile.check()) {
profile.set(getApplicationContext());
}
}
}
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent2 = new Intent(getApplicationContext(), Profiler.class);
PendingIntent pIntent = PendingIntent.getService(getApplicationContext(), ALARM_MANAGER_ID, intent2, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, DateTime.now().plusSeconds(10).getMillis(), pIntent);
}
}
Run Code Online (Sandbox Code Playgroud)
还有一个问题。这种呼叫服务的方法可靠吗? …
我有一个 mongo 查询,我需要分组,然后从最大值中减去最小值,但这样做时遇到问题。
这是我的查询:
{
$group : {
_id : {
type: "$type",
id : "$id"
},
sent : {$subtract: [{$max : "$value"}, {$min : "$value"}]}
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到此错误消息:
$subtract 累加器是一个一元运算符
我想知道如何在小组赛中进行减法。