小编Dra*_*ved的帖子

使用java nio将String写入文件的最佳方法

我需要使用java nio将大字符串写入(追加)平面文件.编码为ISO-8859-1.

目前我们写的如下所示.有没有更好的方法来做同样的事情?

public void writeToFile(Long limit) throws IOException{
     String fileName = "/xyz/test.txt";
     File file = new File(fileName);        
     FileOutputStream fileOutputStream = new FileOutputStream(file, true);  
     FileChannel fileChannel = fileOutputStream.getChannel();
     ByteBuffer byteBuffer = null;
     String messageToWrite = null;
     for(int i=1; i<limit; i++){
         //messageToWrite = get String Data From database
         byteBuffer = ByteBuffer.wrap(messageToWrite.getBytes(Charset.forName("ISO-8859-1")));
         fileChannel.write(byteBuffer);         
     }
     fileChannel.close();
}
Run Code Online (Sandbox Code Playgroud)

编辑:尝试了两种选择.以下是结果.

@Test
public void testWritingStringToFile() {
    DiagnosticLogControlManagerImpl diagnosticLogControlManagerImpl = new DiagnosticLogControlManagerImpl();
    try {
        File file = diagnosticLogControlManagerImpl.createFile();
        long startTime = System.currentTimeMillis();
        writeToFileNIOWay(file);
        //writeToFileIOWay(file);
        long …
Run Code Online (Sandbox Code Playgroud)

java file-io nio character-encoding

37
推荐指数
3
解决办法
7万
查看次数

在反应js中声明变量的位置

我试图在react-js类中声明一个变量.变量应该可以在不同的函数中访问.这是我的代码

class MyContainer extends Component {
    constructor(props) {
        super(props);
        this.testVarible= "this is a test";  // I declare the variable here
    }
    onMove() {
        console.log(this.testVarible); //I try to access it here
    }
}
Run Code Online (Sandbox Code Playgroud)

在onMove上,this.test的值未定义.我知道我可以将值放在状态上,但我不想这样做,因为每次值改变时,都会调用render(),这是不必要的.我是新手做出反应,我做错了吗?

javascript reactjs

30
推荐指数
2
解决办法
8万
查看次数

如何使用gson调用默认的反序列化

我得到一个有"场"字段的json.
如果"字段"有数据,那么有一个OBJECT有许多(大约20个)其他字段也是对象.我可以毫无问题地反序列化它们.
但是如果"field"没有数据,那么它就是一个空的ARRAY(我知道它很疯狂,但那是服务器的响应而我无法改变它).像这样的东西:

空的时候:

"extras":[

]
Run Code Online (Sandbox Code Playgroud)

有一些数据:

"extras": {
    "22":{ "name":"some name" },
    "59":{ "name":"some other name" },
    and so on...
}
Run Code Online (Sandbox Code Playgroud)

所以,当没有数据(空数组)时,我显然得到了异常

Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 4319
Run Code Online (Sandbox Code Playgroud)

我尝试使用自定义JavaDeserializer:

public class ExtrasAdapter implements JsonDeserializer<Extras> {
    @Override
    public Extras deserialize(JsonElement json, Type typeOf,
        JsonDeserializationContext context) throws JsonParseException {
        try {
            JsonObject jsonObject = json.getAsJsonObject();
            // deserialize normally

            // the following does not work, as it makes recursive calls
            // to the same …
Run Code Online (Sandbox Code Playgroud)

java android json gson

28
推荐指数
2
解决办法
1万
查看次数

VB.NET Switch语句GoTo案例

我在VB.NET中编写了一些使用switch语句的代码,但在其中一种情况下,它需要跳转到另一个块.在C#中,它看起来像这样:

switch (parameter)
{
    case "userID":
        // does something here.
    case "packageID":
        // does something here.
    case "mvrType":
        if (otherFactor)
        {
            // does something here.
        }
        else
        {
            goto default;
        }
    default:
        // does some processing...
        break;
}
Run Code Online (Sandbox Code Playgroud)

但是,我不知道如何将其转换为VB.NET.我试过这个:

Select Case parameter 
    Case "userID"
        ' does something here.
    Case "packageID"
        ' does something here.
    Case "mvrType" 
        If otherFactor Then 
            ' does something here. 
        Else 
            GoTo Case Else 
        End If 
    Case Else 
        ' does some processing... 
        Exit Select 
End Select     
Run Code Online (Sandbox Code Playgroud)

但是,当我这样做时,我得到一个编译错误:"标识符预期"."案例"下有一条波浪线.有任何想法吗? …

vb.net goto switch-statement

27
推荐指数
5
解决办法
19万
查看次数

Jquery排序表数据

我在使用jquery对表中的tds进行排序时感到震惊.

我的演示小提琴

如何在我的项目中为任何具有id的表调用它?

var $sort = this;
var $table = $('#mytable');
var $rows = $('tbody &gt; tr',$table);
$rows.sort(function(a, b) {
    var keyA = $('td:eq(0)',a).text();
    var keyB = $('td:eq(0)',b).text();
    if($($sort).hasClass('asc')) {
        return (keyA &gt; keyB) ? 1 : 0;
    } else {
        return (keyA &lt; keyB) ? 1 : 0;
    }
});
Run Code Online (Sandbox Code Playgroud)

sorting jquery

26
推荐指数
2
解决办法
4万
查看次数

如何阻止Google+登录按钮弹出消息"欢迎回来,您已通过Google+登录与此应用程序相关联......"

我正在使用服务器端流程将Google+登录按钮添加到我的网站.以下是我如何呈现登录按钮:

<script type="text/javascript">
    (function () {
        var po = document.createElement('script');
        po.type = 'text/javascript';
        po.async = true;
        po.src = 'https://plus.google.com/js/client:plusone.js?onload=renderGPlus';
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(po, s);
    })();
</script>

<script type="text/javascript">
    function renderGPlus() {
        gapi.signin.render('customGPlusBtn', {
            'callback': 'gPlusSignInCallback',
            'clientid': '<my_client_id>',
            'redirecturi': 'postmessage',
            'accesstype': 'offline',
            'cookiepolicy': 'single_host_origin',
            'requestvisibleactions': 'http://schemas.google.com/BuyActivity',
            'scope': 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email'
        });
    }
</script>
Run Code Online (Sandbox Code Playgroud)

加载按钮后,它会立即检查用户是否已授权我的应用程序(立即模式).如果用户之前已经授权我的应用程序,则会在页面底部弹出一个通知栏,其中显示消息"欢迎回来,您已通过Google+登录与此应用程序连接.....".

在此输入图像描述

反正有没有阻止此消息弹出?

google-plus google-oauth

16
推荐指数
2
解决办法
8936
查看次数

带有文件上传的AngularJs Ajax POST表单

我正在尝试使用ajax请求设置要使用Ajax构建的api提交的表单.由于某种原因,文件只是不想传输到系统,虽然已经有一个后端构建来处理这个,它工作正常.

根据我在这里找到的教程,我的服务看起来像这样:http://badwing.com/multipart-form-data-ajax-uploads-with-angularjs/

addActivity: function(url){
    return $http({
        method: 'POST',
        url: REQUEST_URL + 'Volunteering/AddActivity?token=' + token + url,
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        data: {
            file: $scope.file
        },
        transformRequest: formDataObject
    }).
    then(function(result) {
        console.log(result);
        return result.data;
    });
},
Run Code Online (Sandbox Code Playgroud)

我有一种感觉,这只是一件非常小的东西,我不知道,有人可以提供一些帮助吗?

javascript ajax angularjs

16
推荐指数
2
解决办法
3万
查看次数

应用插件时出错:'com.google.gms.google-services'

我按照谷歌文档将我的应用程序集成到谷歌分析.但是在添加时

apply plugin: 'com.google.gms.google-services'
Run Code Online (Sandbox Code Playgroud)

并构建我的应用程序,我遇到了这个错误:

Error:(49, 0) For input string: "+"
Run Code Online (Sandbox Code Playgroud)

这些是我在应用程序的build.gradle中使用的设置:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
    defaultConfig {
        applicationId "com.myapp.xyz"
        manifestPlaceholders = [
            manifestApplicationId : "${applicationId}",
            onesignal_app_id : "ccd48c54-2069-41f9-8ff7-54c7a12f2d18a",
            onesignal_google_project_number: "306632981237"
        ]
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
            'proguard-rules.pro'
        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    // compile 'com.android.support:appcompat-v7:22.2.1'
    compile project(':facebook')
    compile 'com.android.support:design:22.2.1'
    compile 'com.android.support:palette-v7:22.2.1'
    compile 'com.astuetz:pagerslidingtabstrip:1.0.1' …
Run Code Online (Sandbox Code Playgroud)

android google-analytics-firebase

16
推荐指数
2
解决办法
6万
查看次数

Mono Bug:幻数错误:542

我正在尝试在Linux上的Rider中编译C#Hello World应用程序.当我尝试运行应用程序时,我会遇到以下异常:

Unhandled Exception:
System.TypeInitializationException: The type initializer for 'System.Console' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.ConsoleDriver' threw an exception. ---> System.Exception: Magic number is wrong: 542
at System.TermInfoReader.ReadHeader (System.Byte[] buffer, System.Int32& position) [0x00028] in <a84b655e5e6a49ee96b338ec792f5580>:0
at System.TermInfoReader..ctor (System.String term, System.String filename) [0x0005f] in <a84b655e5e6a49ee96b338ec792f5580>:0
at System.TermInfoDriver..ctor (System.String term) [0x00055] in <a84b655e5e6a49ee96b338ec792f5580>:0
at System.ConsoleDriver.CreateTermInfoDriver (System.String term) [0x00000] in <a84b655e5e6a49ee96b338ec792f5580>:0
at System.ConsoleDriver..cctor () [0x0004d] in <a84b655e5e6a49ee96b338ec792f5580>:0
--- End of inner exception stack trace ---
at System.Console.SetupStreams (System.Text.Encoding inputEncoding, …
Run Code Online (Sandbox Code Playgroud)

c# mono

13
推荐指数
1
解决办法
7562
查看次数

使用flush()方法对每100行10 000个慢速事务进行处理

我有一个示例项目使用spring-bootwith spring-data-jpapostgres db一个表.

我正在尝试将INSERT10 000条记录循环到表中并测量执行时间 - 为每100条记录启用或禁用类的flush()方法EntityManager.

预期的结果是启用flush()方法的执行时间比使用禁用的方法要少得多,但实际上我得到了相反的结果.

UserService.java

package sample.data;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    UserRepository userRepository;

    public User save(User user) {
        return userRepository.save(user);
    }
}
Run Code Online (Sandbox Code Playgroud)

UserRepository.java

package sample.data;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> { }
Run Code Online (Sandbox Code Playgroud)

Application.java

package sample;

import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;

import sample.data.User; …
Run Code Online (Sandbox Code Playgroud)

java flush entitymanager spring-data-jpa spring-boot

13
推荐指数
2
解决办法
1449
查看次数