quick cocos 讀取檔案

2021-07-24 23:56:46 字數 2726 閱讀 9460

最近寫專案的時候有個需求是讓乙個node按照指定的軌跡行走

這就要求讀取本地的檔案,我們讀取的是.dat檔案

0,1,2,3

(1487,-197,341.291199,-2)

(1486,-193,341.291199,-2)

(1485,-189,341.291199,-2)

(1484,-185,341.291199,-2)

(1483,-181,341.291199,-2)

(1482,-177,341.291199,-2)

(1481,-173,341.291199,-2)

(1480,-169,341.291199,-2)

(1479,-165,341.291199,-2)

(1478,-161,341.291199,-2)

(1477,-157,341.291199,-2)

(1476,-153,341.291199,-2)

(1475,-149,341.291199,-2)

(1474,-145,341.291199,-2)

(1473,-141,341.291199,-2)

(1472,-137,341.291199,-2)

(1471,-133,341.291199,-2)

(1470,-129,341.291199,-2)

(1469,-125,341.291199,-2)

(1468,-121,341.291199,-2)

(1467,-117,341.291199,-2)

(1466,-113,341.291199,-2)

(1465,-109,341.291199,-2)

(1464,-105,341.291199,-2)

(1463,-101,341.291199,-2)

(1462,-97,341.291199,-2)

(1461,-93,341.291199,-2)

以下是讀取檔案的lua實現

local fishtrack = {}

function fishtrack.splitstring(_sstring, _sseparator)

local tsplitstring = {}

local isplitindex = 1

local ifindstartindex = 1

while true do

local _ifindlastindex = string.find(_sstring, _sseparator, ifindstartindex)

if not _ifindlastindex then

tsplitstring[isplitindex] = string.sub(_sstring, ifindstartindex, string.len(_sstring))

break

endtsplitstring[isplitindex] = string.sub(_sstring, ifindstartindex, _ifindlastindex - 1)

ifindstartindex = _ifindlastindex + string.len(_sseparator)

isplitindex = isplitindex + 1

endreturn tsplitstring

endfunction fishtrack.readfile(_sfilepath)

return cc.fileutils:getinstance():getstringfromfile(_sfilepath)

endreturn fishtrack

demo:

local mainscene = class("mainscene", function()

return display.newscene("mainscene")

end)

function mainscene:ctor()

local scontent = fishtrack.readfile("4.dat")

local tstrings = fishtrack.splitstring(scontent, "\n")

local ssecondline = string.sub(tstrings[2],2,string.len(tstrings[2])-2)

print(ssecondline)

local tsinglestrings = fishtrack.splitstring(ssecondline,",")

print"*****==="

for k,v in pairs(tsinglestrings) do

print(v)

endfor k,v in pairs(tstrings) do

print(v)

endcc.ui.uilabel.new()

:align(display.center, display.cx, display.cy)

:addto(self)

endfunction mainscene:onenter()

endfunction mainscene:onexit()

endreturn mainscene

以下為輸出

讀取配置檔案並讀取檔案

配置檔案 url users z ten documents name myfile.txt url是讀寫檔案的檔案位址,name是讀寫檔案的檔名稱 fileinputstream is null try catch filenotfoundexception e2 properties pt ne...

Java檔案讀取 jar包內檔案讀取

最近遇到一些jar包內外配置檔案讀取的問題。索性自己測試總結一下,與大家分享。主要是關於classloader.getresource和class.getresource方法路徑區別的問題。1.絕對路徑檔案讀取,最簡單,最直接的方式 從絕對路徑讀取檔案,最基本的檔案讀取方式 author lihzh...

C 讀取檔案

using system using system.collections.generic using system.componentmodel using system.data using system.drawing using system.text using system.window...