Java 字符串在打印时终止

Posted

技术标签:

【中文标题】Java 字符串在打印时终止【英文标题】:Java string is terminated when its printed 【发布时间】:2014-05-22 10:07:40 【问题描述】:

我有一个包含大量数据的字符串

String string = "afsa fd fdsfdsfdsfdsfdsfds fdsfds fdsf dsfds fdsfds fdsf dsfdsf dsfdsfsdfsdf  JsonStr [\"apk_name\":\"android System\",\"apk_package\":\"android\",\"apk_versioncode\":17,\"apk_name\":\"Bubble\",\"apk_package\":\"bz.ktk.bubble\",\"apk_versioncode\":21,\"apk_name\":\"Kingsoft Office\",\"apk_package\":\"cn.wps.moffice_eng\",\"apk_versioncode\":74,\"apk_name\":\"Math Workout\",\"apk_package\":\"com.akbur.mathsworkout\",\"apk_versioncode\":118,\"apk_name\":\"Apollo\",\"apk_package\":\"com.andrew.apollo\",\"apk_versioncode\":2,\"apk_name\":\"Tags\",\"apk_package\":\"com.android.apps.tag\",\"apk_versioncode\":101,\"apk_name\":\"com.android.backupconfirm\",\"apk_package\":\"com.android.backupconfirm\",\"apk_versioncode\":17,\"apk_name\":\"Bluetooth Share\",\"apk_package\":\"com.android.bluetooth\",\"apk_versioncode\":17,\"apk_name\":\"Browser\",\"apk_package\":\"com.android.browser\",\"apk_versioncode\":17,\"apk_name\":\"Calculator\",\"apk_package\":\"com.android.calculator2\",\"apk_versioncode\":17,\"apk_name\":\"Calendar\",\"apk_package\":\"com.android.calendar\",\"apk_versioncode\":17,\"apk_name\":\"Cell Broadcasts\",\"apk_package\":\"com.android.cellbroadcastreceiver\",\"apk_versioncode\":17,\"apk_name\":\"Certificate Installer\",\"apk_package\":\"com.android.certinstaller\",\"apk_versioncode\":17,\"apk_name\":\"Chrome\",\"apk_package\":\"com.android.chrome\",\"apk_versioncode\":1916122,\"apk_name\":\"Contacts\",\"apk_package\":\"com.android.contacts\",\"apk_versioncode\":17,\"apk_name\":\"Package Access Helper\",\"apk_package\":\"com.android.defcontainer\",\"apk_versioncode\":17,\"apk_name\":\"Clock\",\"apk_package\":\"com.android.deskclock\",\"apk_versioncode\":203,\"apk_name\":\"Dev Tools\",\"apk_package\":\"com.android.development\",\"apk_versioncode\":1,\"apk_name\":\"Basic Daydreams\",\"apk_package\":\"com.android.dreams.basic\",\"apk_versioncode\":17,\"apk_name\":\"Photo Screensavers\",\"apk_package\":\"com.android.dreams.phototable\",\"apk_versioncode\":17,\"apk_name\":\"Email\",\"apk_package\":\"com.android.email\",\"apk_versioncode\":410000,\"apk_name\":\"Exchange Services\",\"apk_package\":\"com.android.exchange\",\"apk_versioncode\":500000,\"apk_name\":\"Face Unlock\",\"apk_package\":\"com.android.facelock\",\"apk_versioncode\":17,\"apk_name\":\"Black Hole\",\"apk_package\":\"com.android.galaxy4\",\"apk_versioncode\":1,\"apk_name\":\"Gallery\",\"apk_package\":\"com.android.gallery3d\",\"apk_versioncode\":40001,\"apk_name\":\"html Viewer\",\"apk_package\":\"com.android.htmlviewer\",\"apk_versioncode\":17,\"apk_name\":\"Input Devices\",\"apk_package\":\"com.android.inputdevices\",\"apk_versioncode\":17,\"apk_name\":\"Android keyboard (AOSP)\",\"apk_package\":\"com.android.inputmethod.latin\",\"apk_versioncode\":17,\"apk_name\":\"Key Chain\",\"apk_package\":\"com.android.keychain\",\"apk_versioncode\":17,\"apk_name\":\"Fused Location\",\"apk_package\":\"com.android.location.fused\",\"apk_versioncode\":17,\"apk_name\":\"Magic Smoke Wallpapers\",\"apk_package\":\"com.android.magicsmoke\",\"apk_versioncode\":17,\"apk_name\":\"Messaging\",\"apk_package\":\"com.android.mms\",\"apk_versioncode\":17,\"apk_name\":\"Music Visualization Wallpapers\",\"apk_package\":\"com.android.musicvis\",\"apk_versioncode\":17,\"apk_name\":\"Nfc Service\",\"apk_package\":\"com.android.nfc\",\"apk_versioncode\":17,\"apk_name\":\"Bubbles\",\"apk_package\":\"com.android.noisefield\",\"apk_versioncode\":1,\"apk_name\":\"Package installer\",\"apk_package\":\"com.android.packageinstaller\",\"apk_versioncode\":17,\"apk_name\":\"Phase Beam\",\"apk_package\":\"com.android.phasebeam\",\"apk_versioncode\":1,\"apk_name\":\"Phone\",\"apk_package\":\"com.android.phone\",\"apk_versioncode\":17,\"apk_name\":\"Search Applications Provider\",\"apk_package\":\"com.android.providers.applications\",\"apk_versioncode\":17,\"apk_name\":\"Calendar Storage\",\"apk_package\":\"com.android.providers.calendar\",\"apk_versioncode\":17,\"apk_name\":\"Contacts Storage\",\"apk_package\":\"com.android.providers.contacts\",\"apk_versioncode\":17,\"apk_name\":\"Download Manager\",\"apk_package\":\"com.android.providers.downloads\",\"apk_versioncode\":17,\"apk_name\":\"Downloads\",\"apk_package\":\"com.android.providers.downloads.ui\",\"apk_versioncode\":17,\"apk_name\":\"DRM Protected Content Storage\",\"apk_package\":\"com.android.providers.drm\",\"apk_versioncode\":17,\"apk_name\":\"Media Storage\",\"apk_package\":\"com.android.providers.media\",\"apk_versioncode\":511,i am still testing this :) ";

当我像这样打印这个字符串时

    System.out.println(TAG + string);

字符串在控制台被截断,为什么会这样?

【问题讨论】:

你用谷歌搜索了这个问题吗?试试这些 SO 链接 ***.com/questions/8888654/… 和 ***.com/questions/7606077/… 【参考方案1】:

System.out 被重定向到 logcat。 Logcat 消息的最大长度约为 1k,多余的字符会被截断。

如果您需要记录更长的消息,请使用您自己的日志记录/文件写入解决方案。

【讨论】:

谢谢@laalto 我不知道记录/文件写入,请告诉我 你说什么? @laalto 我也试过同样的效果 Log.d 也会写入 logcat。基本文件 I/O 是您应该构建的基础。

以上是关于Java 字符串在打印时终止的主要内容,如果未能解决你的问题,请参考以下文章

通过递归的方式将字符串逆置打印

python入门基础语法总结

osx上的c ++未捕获异常

字符串标记化在读取整个字符串之前终止

C Socket Send() 直到程序终止才发送

Java进程获取输出并设置超时