无法在Android Studio中获取绝对路径和打开文件

时间:2015-09-27 01:22:50

标签: java android file path filereader

我正在尝试在Android Studio中打开一个文件。我创建了一个具有路径的资产文件夹:

var app                 = require('http').createServer(handler),
    io                  = require('socket.io').listen(app),
    fs                  = require('fs'),
    mysql               = require('mysql'),
    connectionsArray    = [],
    connection          = mysql.createConnection({
        host        : '46.105.14.241',
        user        : 'user',
        password    : 'password',
        database    : 'database',
        port        : 2083
    }),
    POLLING_INTERVAL = 3000,
    pollingTimer;

// If there is an error connecting to the database
connection.connect(function(err) {
  // connected! (unless `err` is set)
  console.log( err );
});

// create a new nodejs server ( http://46.101.226.135:8000/ )
app.listen(8000);

// on server ready we can load our client.html page
function handler ( req, res ) {
    fs.readFile( __dirname + '/client.html' , function ( err, data ) {
        if ( err ) {
            console.log( err );
            res.writeHead(500);
            return res.end( 'Error loading client.html' );
        }
        res.writeHead( 200 );
        res.end( data );
    });
}

/*
*
* HERE IT IS THE COOL PART
* This function loops on itself since there are sockets connected to the page
* sending the result of the database query after a constant interval
*
*/
var pollingLoop = function () {

    // Make the database query
    var query = connection.query('SELECT * FROM test_payout'),
        test_payout = []; // this array will contain the result of our db query


    // set up the query listeners
    query
    .on('error', function(err) {
        // Handle error, and 'end' event will be emitted after this as well
        console.log( err );
        updateSockets( err );

    })
    .on('result', function( user ) {
        // it fills our array looping on each user row inside the db
        test_payout.push( user );
    })
    .on('end',function(){
        // loop on itself only if there are sockets still connected
        if(connectionsArray.length) {
            pollingTimer = setTimeout( pollingLoop, POLLING_INTERVAL );

            updateSockets({test_payout:test_payout});
        }
    });

};

// create a new websocket connection to keep the content updated without any AJAX request
io.sockets.on( 'connection', function ( socket ) {

    console.log('Number of connections:' + connectionsArray.length);
    // start the polling loop only if at least there is one user connected
    if (!connectionsArray.length) {
        pollingLoop();
    }

    socket.on('disconnect', function () {
        var socketIndex = connectionsArray.indexOf( socket );
        console.log('socket = ' + socketIndex + ' disconnected');
        if (socketIndex >= 0) {
            connectionsArray.splice( socketIndex, 1 );
        }
    });

    console.log( 'A new socket is connected!' );
    connectionsArray.push( socket );

});

var updateSockets = function ( data ) {
    // store the time of the latest update
    data.time = new Date();
    // send new data to all the sockets connected
    connectionsArray.forEach(function( tmpSocket ){
        tmpSocket.volatile.emit( 'notification' , data );
    });
};

当我尝试使用

打开它时
app_name\app\src\main\assets\file.csv

我收到错误,找不到该文件。为了尝试找出获取文件的根目录的位置,我使用了以下内容:

FileReader file = new FileReader("assets/PlayerDB.csv");

但是,这只是将“test / test.txt”打印到日志中。关于如何获得绝对工作的任何想法&如何从assets文件夹中打开文件?

谢谢!

2 个答案:

答案 0 :(得分:1)

将您的PlayerDB.csv文件放在Android项目下的/ assets目录中。使用AssetManager类来访问它。

AssetManager am = context.getAssets();
InputStream is = am.open("PlayerDB.csv");

如果您在片段中使用它

AssetManager am = getActivity().getAssets();
InputStream is = am.open("PlayerDB.csv");

答案 1 :(得分:0)

简单地说就是

getAssets().open("file.csv");