25.Java:把一个整数转成字符串

将一个整数转成字符串共有3种方法,如下所示:

示例

public class IntToString {
    
    
    public static void main(String[] args) {
    
    
        Integer num = 456;

        //方式1
        String str1 = String.valueOf(num);
        System.out.println("方式1的结果:" + str1);

        //方式2
        String str2 = Integer.toString(num);
        System.out.println("方式1的结果:" + str2);

        //方式3
        String str3 = num + "";
        System.out.println("方式1的结果:" + str3);
    }
}

实例

读取指定路径下文件长度。当读取的byte异常时,打印出对应的路径 和此时的 byte 数量

		
	private void readTile(int id) {
    
    

		if (Utils.map.containsKey(id)) {
    
    
			return;
		}

		if (!files.containsKey(id)) 
		{
    
    
			return; 
		}

		byte[] curFrameBytes = null;

		File f = files.get(id); 
		try {
    
    

			curFrameBytes = Files.readAllBytes(f.toPath()); 

		} catch (IOException e) {
    
    

			System.out.println("Main thread: Frame Buffer exception: " + e.getMessage());
			e.printStackTrace();
			
		}catch(OutOfMemoryError e){
    
    
			System.out.println("Main thread: OutOfMemoryError: " + e.getMessage());
			e.printStackTrace();
			
		}catch(SecurityException e) {
    
    
			System.out.println("Main thread: SecurityException: " + e.getMessage());
			e.printStackTrace();
		}
		

		if (id != -1) {
    
    
			if (curFrameBytes.length <= 0) {
    
    
				//打印出对应的路径 和此时的 byte 数量
				System.out.println(f.toPath() + " " + curFrameBytes.length);
			}

			Utils.map.put(id, curFrameBytes);
			Utils.id2size.put(id, curFrameBytes.length);// unit : byte 
			readTiles.add(id);

		} 
	}

猜你喜欢

转载自blog.csdn.net/u014217137/article/details/128087240