Questions tagged «java»

Java是一种流行的高级编程语言。如果您在使用或理解语言本身时遇到问题,请使用此标签。这个标签很少单独使用,最常与[spring],[spring-boot],[jakarta-ee],[android],[javafx],[gradle]和[maven]结合使用。

4
如何进入不受密码保护的Java密钥库或更改密码?
我正在尝试将受信任的证书导入Java cacerts密钥库,但是我遇到了问题。我试图列出现有的受信任证书,但似乎密钥库没有密码保护。 $ keytool -list -keystore cacerts Enter keystore password: ***************** WARNING WARNING WARNING ***************** * The integrity of the information stored in your keystore * * has NOT been verified! In order to verify its integrity, * * you must provide your keystore password. * ***************** WARNING WARNING WARNING …

7
如何使用Java锁定文件(如果可能)
我有一个使用FileReader打开文件的Java进程。如何防止另一个(Java)进程打开该文件,或者至少通知第二个进程该文件已打开?如果文件打开(这解决了我的问题),这是否使第二个进程自动获得异常?还是我必须在第一个进程中使用某种标记或参数来显式打开它? 澄清: 我有一个Java应用程序,其中列出了一个文件夹并打开列表中的每个文件进行处理。它依次处理每个文件。每个文件的处理包括读取文件并根据内容进行一些计算,大约需要2分钟。我也有另一个Java应用程序,它执行相同的操作,但写在文件上。我想要的是能够同时运行这些应用程序,这样情况就可以这样。ReadApp列出文件夹并查找文件A,B,C。它将打开文件A并开始读取。WriteApp列出该文件夹并查找文件A,B,C。它将打开文件A,看到已打开(通过异常或其他方式),然后转到文件B。ReadApp完成了文件A,然后继续到B。打开并继续到C。WriteApp不能 在ReadApp读取同一文件时进行写入,反之亦然。它们是不同的过程。
121 java  file-io 

10
复制流,以避免“流已被操作或关闭”
我想复制一个Java 8流,以便可以处理两次。我可以collect列出并从中获得新的信息流; // doSomething() returns a stream List<A> thing = doSomething().collect(toList()); thing.stream()... // do stuff thing.stream()... // do other stuff 但是我认为应该有一种更有效/更优雅的方法。 有没有一种方法可以复制流而不将其转换为集合? 我实际上正在使用Eithers 流,因此想要先处理左侧投影,然后再移至右侧投影并以另一种方式处理。有点像这样(到目前为止,我被迫使用这种toList技巧)。 List<Either<Pair<A, Throwable>, A>> results = doSomething().collect(toList()); Stream<Pair<A, Throwable>> failures = results.stream().flatMap(either -> either.left()); failures.forEach(failure -> ... ); Stream<A> successes = results.stream().flatMap(either -> either.right()); successes.forEach(success -> ... …



14
如何获取一天的开始时间和结束时间?
如何获取一天的开始时间和结束时间? 像这样的代码是不正确的: private Date getStartOfDay(Date date) { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DATE); calendar.set(year, month, day, 0, 0, 0); return calendar.getTime(); } private Date getEndOfDay(Date date) { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int …
121 java  date 

12
将时间设置为00:00:00
我在用Java重置小时数时遇到问题。对于给定的日期,我要将小时设置为00:00:00。 这是我的代码: /** * Resets milliseconds, seconds, minutes and hours from the provided date * * @param date * @return */ public static Date trim(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.HOUR, 0); return calendar.getTime(); } 问题是,时间有时是这样12:00:00,有时是这样00:00:00,当我查询数据库中保存的实体以及查询07.02.2013 00:00:00的实际实体时间即存储12:00:00失败时。 我知道12:00:00 == 00:00:00! 我正在使用AppEngine。这是Appengine错误,问题还是其他问题?还是取决于其他因素?


2
Boolean.hashCode()
hashCode()布尔类的方法是这样实现的: public int hashCode() { return value ? 1231 : 1237; } 为什么使用1231和1237?为什么不别的呢?
121 java  boolean  hashcode 



7
是否可以从InputStream创建File对象
有什么方法可以从中创建java.io.File对象java.io.InputStream吗? 我的要求是从RAR读取文件。我不是要写一个临时文件,而是要在RAR存档中找到一个文件。
121 java  io 

9
Java ByteBuffer转为字符串
这是将ByteBuffer转换为String的正确方法吗? String k = "abcd"; ByteBuffer b = ByteBuffer.wrap(k.getBytes()); String v = new String(b.array()); if(k.equals(v)) System.out.println("it worked"); else System.out.println("did not work"); 我问的原因是,这看起来太简单了,而其他方法,例如Java:在ByteBuffer和ByteBuffer之间来回转换字符串以及相关的问题看起来就更加复杂。

3
在Java8中使用时区格式化LocalDateTime
我有这个简单的代码: DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss.SSSSSS Z"); LocalDateTime.now().format(FORMATTER) 然后我将得到以下异常: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds at java.time.LocalDate.get0(LocalDate.java:680) at java.time.LocalDate.getLong(LocalDate.java:659) at java.time.LocalDateTime.getLong(LocalDateTime.java:720) at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298) at java.time.format.DateTimeFormatterBuilder$OffsetIdPrinterParser.format(DateTimeFormatterBuilder.java:3315) at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2182) at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1745) at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1719) at java.time.LocalDateTime.format(LocalDateTime.java:1746) 如何解决这个问题?
121 java  java-8  java-time 

3
获取符合条件的第一个元素
如何获取与流中的条件匹配的第一个元素?我已经尝试过了但是没用 this.stops.stream().filter(Stop s-> s.getStation().getName().equals(name)); 该条件不起作用,在除Stop之外的其他类中调用filter方法。 public class Train { private final String name; private final SortedSet<Stop> stops; public Train(String name) { this.name = name; this.stops = new TreeSet<Stop>(); } public void addStop(Stop stop) { this.stops.add(stop); } public Stop getFirstStation() { return this.getStops().first(); } public Stop getLastStation() { return this.getStops().last(); } …
121 java  java-8  java-stream 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.