Javainheritance示例

以下是inheritance的示例 class Parent { Parent(int a, int b) { int c = a + b; System.out.println(“Sum=” + c); } void display() { System.out.println(“Return Statement”); } } class Child extends Parent { Child(int a, int b) { int c = a – b; System.out.println(“Difference=” + c); } } public class InheritanceExample { public static void main(String […]

如何使用Content-Encoding读取压缩的HTML页面:gzip

我请求一个发送Content-Encoding:gzip标头的网页,但是卡住了怎么读它.. 我的代码: try { URLConnection connection = new URL(“http://jquery.org”).openConnection(); String html = “”; BufferedReader in = null; connection.setReadTimeout(10000); in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null){ html+=inputLine+”\n”; } in.close(); System.out.println(html); System.exit(0); } catch (IOException ex) { Logger.getLogger(Crawler.class.getName()).log(Level.SEVERE, null, ex); } 输出看起来很乱。(我无法在这里粘贴它,一种符号……) 我相信这是一个压缩内容,如何解析呢? 注意: 如果我将jquery.org更改为jquery.com(不发送该标题,我的代码运行良好)

带抽象参数和inheritance的Java抽象方法

我最近遇到了一个API和一个实现的问题,其中出现了以下类型的代码: API是一个抽象类: public abstract class A { public A sum(A a) { System.out.println(“A.sum(A) called”); return null; } } 实现是一个简单的类: public class B extends A { public B sum(B b) { System.out.println(“B.sum(B) called”); return null; } } 说到使用它我写道: public class Main { public static void main(String args[]) { B b = new B(); A basa = […]

“比较方法违反其一般合同”仅在某些情况下被抛出

首先,我知道许多其他线程都描述了这个问题。 但是我无法找到并回答这个问题,为什么不总是抛出这个错误? 让我来描述一下我的意思。 我已经写了一些示例代码来说明这一点: public class Mushroom { public int size; public Mushroom(int size) { this.size = size; } @Override public boolean equals(Object obj) { //this is intentionally false – read in description return false; } } DSA public class MushroomComparator implements Comparator { @Override public int compare(Mushroom o1, Mushroom o2) { // here is […]

java.util.concurrent.Future中的方法cancel()是否会阻塞?

我正在尝试在我的项目中实现Future 接口。 但看起来文档对它来说有点模糊。 从官方文档我们可以推断出: 方法cancel()不会抛出InterruptedException或ExecutionException等exception。 此外,它没有超时的变体。 所以看起来,它不应该阻止。 文档说 此方法返回后,对isDone()的后续调用将始终返回true。 但 boolean isDone()如果此任务已完成,则返回true。 因此,如果我们在任务处理时运行cancel()并且无法取消,则此方法应该等到任务完成。 哪个与1相矛盾。 cancel()的返回值描述为 返回:如果无法取消任务,则返回false,通常是因为它已经正常完成; 否则是真的 因此,如果任务正在运行并且可能被取消但不是在这个确切的时刻,我们应该返回true(我们不能声明它不能被取消)或等待(但它与1相矛盾)。 但也有一个声明 如果此方法返回true,则对isCancelled()的后续调用将始终返回true。 但 boolean isCancelled()如果此任务在正常完成之前被取消,则返回true。 当我们在任务运行时运行cancel()并且不能说任务是否可以被取消时(因为在这种情况下cancel()应该返回true,但是isCancelled()应该返回false),这与3相矛盾。 看起来很久以前这个API已经被解除了,并且这些不一致不应该出现在文档中。 但那里有。 我不明白了吗?

如何知道HTTP服务器何时完成发送数据

我正在开发一个面向浏览器/代理的项目,我需要下载网页。 在向Web服务器发送自定义HTTP请求后,我开始侦听服务器响应。 在阅读响应时,我检查Content-Length的响应头:-row。 如果我得到其中一个,很容易确定服务器何时完成发送数据,因为我总是知道我收到了多少字节的数据。 当服务器不包含Content-Length标头并且还为进一步的请求保持连接打开时,会发生此问题。 例如,Google服务器以gzip-content响应,但不包含内容长度。 我怎么知道何时停止等待更多数据并关闭连接? 我已经考虑过在没有收到数据的情况下使用超时值来关闭连接,但这似乎是错误的方法。 例如,Chrome可以下载与我相同的页面,并且似乎总是知道何时关闭连接。

在java中将byte salt转换为String

我试图将byte []格式的生成salt值转换为String。 但每次它转换为特殊字符。 我使用以下方式将byte []转换为String: byte[] salt = new byte[] { 50, 111, 8, 53, 86, 35, -19, -47 }; String saltString = new String(salt,”UTF-8″); System.out.println(saltString); 但输出就像??@?

在JavaFx中编码UDP通信

我在JavaFx中编码UDP通信时遇到问题。 代码能够发送消息但不能接收消息。 我应该在源代码中修改以接收消息? 这是源代码: public class JavaFXApplication10 extends Application { public DatagramSocket receivesocket; public DatagramPacket receivepacket; public DatagramSocket sendsocket; public DatagramPacket sendpacket; public InetSocketAddress remoteAdress; public Label label_IP; public TextField tx_IP; public Label label_SENDPORT; public Label label_RECEIVEPORT; public TextField tx_SENDPORT; public TextField tx_RECEIVEPORT; public Button bt_co; public Button bt_start ; private String message; private XYChart.Series […]

awtrobot读取错误的颜色

我正在寻找一种独立于平台的方法来从屏幕上读取颜色,并通过鼠标和键盘操作模拟用户输入。 我以前一直在使用java.awt.robot ,但是在过去的两年中它已经开始在OSX中报告错误的颜色(其他操作系统似乎得到准确的颜色),如本问题所述 。 我正在寻找这个bug的解决方法或任何其他将提供此function的库/语言。 使用下面相当简单的代码可以certificate这个问题。 public void printColorAtCoordinate(int x, int y) { Color color; color = robot.getPixelColor(x, y); System.out.println(“RGB: ” + Integer.toHexString(color.getRGB())); } 它似乎得到了一些正确的颜色,例如0x000000 , 0x0000ff , 0xff0000但不是其他如0x00ff00 ,它报告为ff22ff00 (而不是ff00ff00 )。 大多数颜色至少少量。

spring batch exception无法构造java.util.Map $ Entry

从命令行执行spring批处理作业时,我们面临以下exception。 Spring Batch Version – 3.0.2.RELEASE Spring Version – 4.0.0.RELEASE xStream version – 1.4.7 Data Base – mysql(我连接到新的数据库模式。)使用以下命令执行作业时,我遇到exception… java %JAVA_OPTS% org.springframework.batch.core.launch.support.CommandLineJobRunner config/spring-config.xml partitionJdbcJob Exception ———- INFO: Loaded JDBC driver: com.mysql.jdbc.Driver Feb 20, 2015 3:22:28 PM org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet INFO: No TaskExecutor has been set, defaulting to synchronous executor. Feb 20, 2015 3:22:29 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run INFO: […]