برنامه نویسی اندروید

اموزش برنامه نویسی اندروید تخصصی

برنامه نویسی اندروید

اموزش برنامه نویسی اندروید تخصصی

۲ مطلب در تیر ۱۴۰۱ ثبت شده است

تبدیل متغیر int به متغیر String ، در برنامه نویسی اندروید

فرض کنید که یک متغیر int با نام num داریم و می خواهیم که آن را به متغیر String تبدیل کنیم، برای این منظور، کدهای زیر را می نویسیم :

 

int num = 1234;  
String str = String.valueOf(num);

بنابراین یک رشته (String) با نام str خواهیم داشت که مقدار 1234 در آن ذخیره شده است.

  • vahid hasani

 

try {
    // write string to a file
    Files.write(Paths.get("output.txt"), "Hey, there!".getBytes());

} catch (IOException ex) {
    ex.printStackTrace();
}

To specify a different character encoding other than the default UTF-8, you can do the following:

try {
    // create a string list
    List<String> contents = Collections.singletonList("Hey, there!");
    
    // write string to a file
    Files.write(Paths.get("output.txt"), contents,
            StandardCharsets.UTF_16);

} catch (IOException ex) {
    ex.printStackTrace();
}

To create a non-existing file or append the string to an existing one, use the following code snippet:

try {
    // create a string list
    List<String> contents = Collections.singletonList("Hey, there!");

    // write string to a file
    Files.write(Paths.get("output.txt"), contents,
            StandardCharsets.UTF_16,
            StandardOpenOption.CREATE,
            StandardOpenOption.APPEND);

} catch (IOException ex) {
    ex.printStackTrace();
}
  • vahid hasani