java length 长度 java中length()

1 java中的length属性是针对数组说的,比如说你声明了一个数组,想知道这个数组的长度则用到了length这个属性.

2 java中的length()方法是针对字符串String说的,如果想看这个字符串的长度则用到length()这个方法.

3.java中的size()方法是针对泛型集合说的,如果想看这个泛型有多少个元素,就调用此方法来查看!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用串口通讯库来实现Ymodem传输文件。以下是一个简单的示例代码: ```java import java.io.*; import java.util.*; import purejavacomm.*; public class YModem { private static final byte SOH = 0x01; private static final byte STX = 0x02; private static final byte EOT = 0x04; private static final byte ACK = 0x06; private static final byte NAK = 0x15; private static final byte CAN = 0x18; private static final byte CRC16 = 0x43; private static final int BLOCK_SIZE = 1024; private static final int MAX_RETRIES = 10; private static final int FILE_SIZE_OFFSET = 132; private static final int FILE_SIZE_LENGTH = 10; private static final int FILE_NAME_OFFSET = 0; private static final int FILE_NAME_LENGTH = 128; public static void send(String fileName, OutputStream outputStream) throws Exception { File file = new File(fileName); FileInputStream fileInputStream = new FileInputStream(file); long fileSize = file.length(); byte[] fileNameBytes = Arrays.copyOf(file.getName().getBytes("ASCII"), FILE_NAME_LENGTH); byte[] fileSizeBytes = Arrays.copyOf(Long.toString(fileSize).getBytes("ASCII"), FILE_SIZE_LENGTH); int blockNumber = 1; int retries = 0; while (true) { outputStream.write(NAK); int bytesRead = fileInputStream.read(); if (bytesRead != SOH) { throw new Exception("Invalid start of header"); } int blockIndex = 0; byte[] block = new byte[BLOCK_SIZE + 4]; block[blockIndex++] = SOH; block[blockIndex++] = (byte) blockNumber; block[blockIndex++] = (byte) ~blockNumber; bytesRead = fileInputStream.read(block, blockIndex, BLOCK_SIZE); if (bytesRead < BLOCK_SIZE) { Arrays.fill(block, blockIndex + bytesRead, blockIndex + BLOCK_SIZE, (byte) 0x1A); } int crc = calculateCRC16(block, 3, BLOCK_SIZE); block[blockIndex + BLOCK_SIZE] = (byte) ((crc >> 8) & 0xFF); block[blockIndex + BLOCK_SIZE + 1] = (byte) (crc & 0xFF); outputStream.write(block); int response = readByte(inputStream); if (response == ACK) { blockNumber++; if (blockNumber == 256) { blockNumber = 0; } retries = 0; if (bytesRead < BLOCK_SIZE) { break; } } else if (response == NAK) { retries++; if (retries >= MAX_RETRIES) { throw new Exception("Transfer failed"); } } else { throw new Exception("Invalid response: " + response); } } fileInputStream.close(); while (true) { outputStream.write(EOT); int response = readByte(inputStream); if (response == NAK) { continue; } else if (response == ACK) { break; } else { throw new Exception("Invalid response: " + response); } } } public static void receive(String fileName, InputStream inputStream) throws Exception { FileOutputStream fileOutputStream = new FileOutputStream(fileName); byte[] block = new byte[BLOCK_SIZE + 4]; int blockNumber = 1; int retries = 0; boolean endOfFile = false; while (!endOfFile) { block[0] = (byte) inputStream.read(); if (block[0] == EOT) { outputStream.write(ACK); break; } else if (block[0] != SOH && block[0] != STX) { throw new Exception("Invalid start of header"); } int bytesRead = inputStream.read(block, 1, BLOCK_SIZE + 3); if (bytesRead < BLOCK_SIZE + 3) { throw new Exception("Invalid block size"); } int crc = calculateCRC16(block, 3, BLOCK_SIZE); if (crc != ((block[BLOCK_SIZE + 3] << 8) | (block[BLOCK_SIZE + 2] & 0xFF))) { throw new Exception("Invalid CRC"); } if (blockNumber == block[1] && ~blockNumber == block[2]) { retries = 0; fileOutputStream.write(block, 3, BLOCK_SIZE); outputStream.write(ACK); blockNumber++; if (blockNumber == 256) { blockNumber = 0; } } else if (blockNumber == ((block[1] + 1) & 0xFF) && ~(blockNumber) == block[2]) { retries = 0; endOfFile = true; int bytesToWrite = 0; for (int i = 0; i < FILE_SIZE_LENGTH; i++) { if (fileSizeBytes[i] == 0) { break; } bytesToWrite = bytesToWrite * 10 + (fileSizeBytes[i] - '0'); } fileOutputStream.write(block, 3, bytesToWrite); outputStream.write(ACK); } else { retries++; if (retries >= MAX_RETRIES) { throw new Exception("Transfer failed"); } outputStream.write(NAK); } } fileOutputStream.close(); } private static int calculateCRC16(byte[] data, int offset, int length) { int crc = 0; for (int i = offset; i < offset + length; i++) { crc ^= (data[i] & 0xFF) << 8; for (int j = 0; j < 8; j++) { if ((crc & 0x8000) != 0) { crc = (crc << 1) ^ 0x1021; } else { crc <<= 1; } } } return crc & 0xFFFF; } private static int readByte(InputStream inputStream) throws Exception { long startTime = System.currentTimeMillis(); while (inputStream.available() == 0) { if (System.currentTimeMillis() - startTime > 10000) { throw new Exception("Timeout waiting for response"); } } return inputStream.read(); } } ``` 在这个示例代码,send()方法用于发送文件,而receive()方法用于接收文件。如果您使用的是Windows系统,则需要安装Java Communications API(JavaComm)来使用串口通讯库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值