![](/img/trans.png)
[英]Assignments are not expressions and only expressions are allowed in this context - Kotlin
[英]Assignments are not expressions, and only expressions are allowed in this context - Error when convert Java to Kotlin
我有很好的托管代碼和Java
項目。 但我需要在Kotlin
開發另一個項目。 所以,我在Kotlin
盡可能地轉換了所有代碼。 但是有ZipFileManager.kt
代碼用於壓縮/解壓縮文件。
這是代碼( Kotlin
):
object ZipFileManager {
private val BUFFER_SIZE = 6 * 1024
@Throws(IOException::class)
fun zip(files: Array<String>, zipFile: String) {
var origin: BufferedInputStream? = null
val out = ZipOutputStream(BufferedOutputStream(FileOutputStream(zipFile)))
try {
val data = ByteArray(BUFFER_SIZE)
for (file in files) {
val fi = FileInputStream(file)
origin = BufferedInputStream(fi, BUFFER_SIZE)
try {
val entry = ZipEntry(file.substring(file.lastIndexOf("/") + 1))
out.putNextEntry(entry)
var count: Int
while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
out.write(data, 0, count)
}
} finally {
origin.close()
}
}
} finally {
out.close()
}
}
fun unzip(zipFileUrl: String, fileLocation: String) {
try {
val f = File(fileLocation)
if (!f.isDirectory) {
f.mkdirs()
}
ZipInputStream(FileInputStream(zipFileUrl)).use { zin ->
var ze: ZipEntry? = null
while ((ze = zin.nextEntry) != null) {
// Log.e("UnZipFILE", "Unzipping....");
val path = fileLocation + ze!!.name
if (ze.isDirectory) {
val unzipFile = File(path)
if (!unzipFile.isDirectory) {
unzipFile.mkdirs()
}
} else {
FileOutputStream(path, false).use { fout ->
val buffer = ByteArray(1024)
var read: Int
while ((read = zin.read(buffer)) != -1) {
fout.write(buffer, 0, read)
}
zin.closeEntry()
}
}
}
}
} catch (e: Exception) {
e.printStackTrace()
Log.e("UnZipException", Log.getStackTraceString(e))
}
}
}
所以,我正在嘗試通過此代碼,但它顯示編譯時錯誤,如:
Assignments are not expressions, and only expressions are allowed in this context
中Assignments are not expressions, and only expressions are allowed in this context
在行中的fun zip
中while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1)
while ((ze = zin.nextEntry) != null)
和行while ((read = zin.read(buffer)) != -1)
的行處給出另一個相同的編譯時錯誤。
所以,我的大問題是在Kotlin
使用此代碼。 那么,任何身體都可以幫助誰知道Kotlin
以及如何在Kotlin
使用這種類型的循環結構?
如果有人想看到我也有Java
代碼:
public class ZipFileManager {
private static int BUFFER_SIZE = 6 * 1024;
public static void zip(String[] files, String zipFile) throws IOException {
BufferedInputStream origin = null;
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
try {
byte data[] = new byte[BUFFER_SIZE];
for (String file : files) {
FileInputStream fi = new FileInputStream(file);
origin = new BufferedInputStream(fi, BUFFER_SIZE);
try {
ZipEntry entry = new ZipEntry(file.substring(file.lastIndexOf("/") + 1));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
out.write(data, 0, count);
}
} finally {
origin.close();
}
}
} finally {
out.close();
}
}
public static void unzip(String zipFileUrl, String fileLocation) {
try {
File f = new File(fileLocation);
if (!f.isDirectory()) {
f.mkdirs();
}
try (ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFileUrl))) {
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
// Log.e("UnZipFILE", "Unzipping....");
String path = fileLocation + ze.getName();
if (ze.isDirectory()) {
File unzipFile = new File(path);
if (!unzipFile.isDirectory()) {
unzipFile.mkdirs();
}
} else {
try (FileOutputStream fout = new FileOutputStream(path, false)) {
byte[] buffer = new byte[1024];
int read;
while ((read = zin.read(buffer)) != -1) {
fout.write(buffer, 0, read);
}
zin.closeEntry();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
Log.e("UnZipException", Log.getStackTraceString(e));
}
}
}
我也嘗試管理循環:
do {
ze = zin.nextEntry
} while (ze != null)
但是文件沒有正確解壓縮或破壞。 所以,如果有任何機構有想法管理這種類型的循環,那將非常有幫助。
我正在將您的Java
代碼轉換為Kotlin
我之前遇到過這個問題。 Assignments are not expressions, and only expressions are allowed in this context
在這里使用此代碼是您的解決方案
object ZipFileManager {
private val BUFFER_SIZE = 6 * 1024
@Throws(IOException::class)
fun zip(files: Array<String>, zipFile: String) {
var origin: BufferedInputStream? = null
val out = ZipOutputStream(BufferedOutputStream(FileOutputStream(zipFile)))
try {
val data = ByteArray(BUFFER_SIZE)
for (file in files) {
val fi = FileInputStream(file)
origin = BufferedInputStream(fi, BUFFER_SIZE)
try {
val entry = ZipEntry(file.substring(file.lastIndexOf("/") + 1))
out.putNextEntry(entry)
var count: Int= origin.read(data, 0, BUFFER_SIZE);
while (count != -1) {
out.write(data, 0, count)
count = origin.read(data, 0, BUFFER_SIZE)
}
} finally {
origin.close()
}
}
} finally {
out.close()
}
}
fun unzip(zipFileUrl: String, fileLocation: String) {
try {
val f = File(fileLocation)
if (!f.isDirectory) {
f.mkdirs()
}
ZipInputStream(FileInputStream(zipFileUrl)).use { zin ->
var ze: ZipEntry? = null
ze = zin.nextEntry
while (ze != null) {
// Log.e("UnZipFILE", "Unzipping....");
val path = fileLocation + ze!!.name
if (ze.isDirectory) {
val unzipFile = File(path)
if (!unzipFile.isDirectory) {
unzipFile.mkdirs()
}
} else {
FileOutputStream(path, false).use { fout ->
val buffer = ByteArray(1024)
var read: Int= zin.read(buffer)
while (read != -1) {
fout.write(buffer, 0, read)
read = zin.read(buffer)
}
zin.closeEntry()
}
}
ze = zin.nextEntry
}
}
} catch (e: Exception) {
e.printStackTrace()
Log.e("UnZipException", Log.getStackTraceString(e))
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.